I have a hadoop file that has \u001 as a delimiter, and I try to read the data and split records by \u001. But the length is always 1, that is the data is not been spilt.
var fs = require('fs');
var buffer = fs.readFile('20140820075209.txt', function (err, data) {
console.log(data.toString());
console.log(data.toString().split('\\u001').length);
});
You shouldn't escape the backslash if you want the actual character:
console.log(data.toString().split('\u0001').length);
or:
console.log(data.toString().split('\x01').length);
Related
I am trying to simply replace a line in a text file using JavaScript.
The idea is:
var oldLine = 'This is the old line';
var newLine = 'This new line replaces the old line';
Now i want to specify a file, find the oldLine and replace it with the newLine and save it.
Anyone who can help me here?
Just building on Shyam Tayal's answer, if you want to replace an entire line matching your string, and not just an exact matching string do the following instead:
fs.readFile(someFile, 'utf8', function(err, data) {
let searchString = 'to replace';
let re = new RegExp('^.*' + searchString + '.*$', 'gm');
let formatted = data.replace(re, 'a completely different line!');
fs.writeFile(someFile, formatted, 'utf8', function(err) {
if (err) return console.log(err);
});
});
The 'm' flag will treat the ^ and $ meta characters as the beginning and end of each line, not the beginning or end of the whole string.
So the above code would transform this txt file:
one line
a line to replace by something
third line
into this:
one line
a completely different line!
third line
This should do it
var fs = require('fs')
fs.readFile(someFile, 'utf8', function (err,data) {
var formatted = data.replace(/This is the old line/g, 'This new line replaces the old line');
fs.writeFile(someFile, formatted, 'utf8', function (err) {
if (err) return console.log(err);
});
});
I know you can read line-by-line with require('readline'), is there a good way to read a file character by character? Perhaps just use readline and then split the line into characters?
I am trying to convert this code:
const fs = require('fs');
const lines = String(fs.readFileSync(x));
for(const c of lines){
// do what I wanna do with the c
}
looking to make this into something like:
fs.createReadStream().pipe(readCharByChar).on('char', c => {
// do what I wanna do with the c
});
Simple for loop
let data = fs.readFileSync('filepath', 'utf-8');
for (const ch of data){
console.log(ch
}
Using forEach
let data = fs.readFileSync('filepath', 'utf-8');
data.split('').forEach(ch => console.log(ch)
I have a csv file I want to use in node.js/express. How can I convert the file to a variable of type array/json/string. I've tried:
fs.readFile('Resource.csv', function(err, data) {
console.log(data)}
And also tried a number of other things I could find in SO but none work for me. The data is of multiple rows if it matters.
var fs = require('fs');
var data = fs.readFileSync('Resource.csv')
.toString() // convert Buffer to string
.split('\n') // split string to lines
.map(e => e.trim()) // remove white spaces for each line
.map(e => e.split(',').map(e => e.trim())); // split each line to array
console.log(data);
console.log(JSON.stringify(data, '', 2)); // as json
To expand on my comment (from the csvtojson doc)
Install with npm i --save csvtojson
Then you use the module like this :
CSV File :
a,b,c
1,2,3
4,5,6
JS Code :
const csvFilePath='<path to csv file>' // Resource.csv in your case
const csv=require('csvtojson') // Make sure you have this line in order to call functions from this modules
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
console.log(jsonObj);
})
Output :
[
{a:"1", b:"2", c:"3"},
{a:"4", b:"5". c:"6"}
]
I would like add string on the top of my js file. Actuly, it's on the end :
var file = './public/js/app.bundleES6.js',
string = '// My string';
fs.appendFileSync(file, string);
Do you have idea for add my string on the first line ?
Thank you !
I think there is no built-in way to insert at the beginning of the file in Node.js.
But you can use readFileSync and writeFile methods of fs to resolve this issue
It will append string at top of the file
Try this
Method#1
var fs = require('fs');
var data = fs.readFileSync('./example.js').toString().split("\n");
data.splice(0, 0, "Append the string whatever you want at top" );
var text = data.join("\n");
fs.writeFile('./example.js', text, function (err) {
if (err) return err;
});
Method#2
If you are relying on to use third party module then you can use prepend module to add the string at the top as suggested by #robertklep.
var prepend = require('prepend');
prepend(FileName, 'String to be appended', function(error) {
if (error)
console.error(error.message);
});
I want to read the size of bytes of a file as I read it. I have this
var path = 'training_data/dat1.txt';
var fs = require("fs"); //Load the filesystem module
var stats = fs.statSync(path);
var fileSizeInBytes = stats["size"];
var accSize = 0;
var lineReader = require('readline').createInterface({
input: fs.createReadStream(path)
});
lineReader.on('line', function (line) {
accSize += Buffer.byteLength(line, 'utf8');
console.log(accSize + "/" + fileSizeInBytes);
});
lineReader.on('close', function() {
console.log('completed!');
});
But it doesn't print out the correct filesize.
7/166
16/166
23/166
32/166
39/166
48/166
55/166
64/166
71/166
80/166
87/166
96/166
103/166
112/166
it prints this for example.
Does anyone know whats wrong?
The lineReader doesn't include the newline \n character in the buffer as each line is read, which is where your bytes are missing.
Try this:
accSize += Buffer.byteLength(line + '\n', 'utf8');
EDIT
If the file being read uses Windows line endings, you'll need to add two characters, since those will also have a carriage return in addition to line feed, denoted as '\r\n'. (see this for more details)