Reputation: 4358
So I have a massive text file that looks like :
e10_04txyscelsxeu4j63dx49b7nh3dzsn_q33_fdgskfdn_q53
e2_05txyscelsxeu4j63dx49b7nh3dzsn_q11_fdgskfdn_q13
e9_01_1txyscelsxeu4j63dx49b7nh3dzsn_q06_fdgskfdn_q42
e10_23txyscelsxeu4j63dx49b7nh3dzsn_q04_fdgskfdn_q41
There's a list of these random strings. I'm trying to iterate over each of the lines, grab the first 10
letters/numbers and spit them out somewhere so I can do something with them.
At the moment i'm iterating over every letter like so :
const fs = require('fs');
fs.readFile('myData', 'utf8', (err, data) => {
for (var i = 0, j = data.length; i < j; i++) {
if (i == 10) {
console.log('test');
} else {
}
}
});
Is there anyway I can do what i'm trying to do?
Thanks!
Upvotes: 0
Views: 1757
Reputation: 4217
You should use readline module as @jordanhendrix said.
To grapes first 10 characters for each line, you can use
line.substring(0, 10);
Upvotes: 1
Reputation: 13896
You could read the file one line at a time usingreadline
:
// this creates a read stream
var reader = require('readline').createInterface({
input: require('fs').createReadStream('file.in')
});
// then here you would be able to manipulate each line:
reader.on('line', function (line) {
console.log('The current line is: ', line);
});
Upvotes: 4