Node.js : Determining the line count of a text file

Create a new file as app.js and paste below code –

app.js

const readline = require(‘readline’);
const fs = require(‘fs’);
var file = ‘path.to.file’;
var linesCount = 0;
var rl = readline.createInterface({
input: fs.createReadStream(file),
output:…


This content originally appeared on DEV Community and was authored by Rajesh Kumar Yadav

Create a new file as app.js and paste below code -

app.js

const readline = require('readline');
const fs = require('fs');
var file = 'path.to.file';
var linesCount = 0;
var rl = readline.createInterface({
 input: fs.createReadStream(file),
 output: process.stdout,
 terminal: false
});
rl.on('line', function (line) {
 linesCount++; // on each linebreak, add +1 to 'linesCount'
});
rl.on('close', function () {
 console.log(linesCount); // print the result when the 'close' event is called
});

Run below command -

node app.js

You will get the line count of text file.

Buy Me A Coffee

With all that being said, I highly recommend you keep learning!

Thank you for reading this article. Please feel free to connect with me on LinkedIn and Twitter.


This content originally appeared on DEV Community and was authored by Rajesh Kumar Yadav


Print Share Comment Cite Upload Translate Updates
APA

Rajesh Kumar Yadav | Sciencx (2021-05-20T04:19:29+00:00) Node.js : Determining the line count of a text file. Retrieved from https://www.scien.cx/2021/05/20/node-js-determining-the-line-count-of-a-text-file/

MLA
" » Node.js : Determining the line count of a text file." Rajesh Kumar Yadav | Sciencx - Thursday May 20, 2021, https://www.scien.cx/2021/05/20/node-js-determining-the-line-count-of-a-text-file/
HARVARD
Rajesh Kumar Yadav | Sciencx Thursday May 20, 2021 » Node.js : Determining the line count of a text file., viewed ,<https://www.scien.cx/2021/05/20/node-js-determining-the-line-count-of-a-text-file/>
VANCOUVER
Rajesh Kumar Yadav | Sciencx - » Node.js : Determining the line count of a text file. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/20/node-js-determining-the-line-count-of-a-text-file/
CHICAGO
" » Node.js : Determining the line count of a text file." Rajesh Kumar Yadav | Sciencx - Accessed . https://www.scien.cx/2021/05/20/node-js-determining-the-line-count-of-a-text-file/
IEEE
" » Node.js : Determining the line count of a text file." Rajesh Kumar Yadav | Sciencx [Online]. Available: https://www.scien.cx/2021/05/20/node-js-determining-the-line-count-of-a-text-file/. [Accessed: ]
rf:citation
» Node.js : Determining the line count of a text file | Rajesh Kumar Yadav | Sciencx | https://www.scien.cx/2021/05/20/node-js-determining-the-line-count-of-a-text-file/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.