I have large text files, which range between 30MB
and 10GB
. How can I count the number of lines in a file using Node.js
?
I hav
If you use Node 8 and above, you can use this async/await pattern
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function fileLineCount({ fileLocation }) {
const { stdout } = await exec(`cat ${fileLocation} | wc -l`);
return parseInt(stdout);
};
// Usage
async someFunction() {
const lineCount = await fileLineCount({ fileLocation: 'some/file.json' });
}