I have a csv file in local server.I am trying to get total number of rows in a csvfile at a time.But I am unable to do this.Please help.
Asked
Active
Viewed 6,494 times
0
-
look at this library https://github.com/wdavidw/node-csv – Plato Aug 30 '13 at 19:41
-
Do this maybe: http://stackoverflow.com/a/12453463/711902 – Trevor Dixon Aug 30 '13 at 20:14
-
it does not work properly.I want to get total no of rows in a csv file .In my case there is no "\r" or "\n". – user2700253 Aug 31 '13 at 08:10
2 Answers
1
Used this snippet to calculate line count from Jason Kim's answer and it worked like a charm.
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' });
}
P.S. This works for any type for file not just csv.

Nikhil S
- 35
- 7
0
Work with CSV through a library, as Plato suggested.
FWIW, to get the length of any text file,
var content;
// First I want to read the file
fs.readFile('./local.csv', function read(err, data) {
if (err) {
throw err;
}
content = data;
processFile(); // Or put the next step in a function and invoke it
});
function processFile() {
lines = content.split("\r"); // look up
rowCount = lines.length;
}

Jim Mack
- 1,437
- 11
- 16