Read the last line of a CSV file and extract one value

Posted on

Read the last line of a CSV file and extract one value

New to Node.js and trying to pull a value from the very last line of a CSV file. Here is the CSV:

Unit ID,Date,Time,Audio File
Log File Created,3/6/2013,11:18:25 AM,file:\C:UsersBenDocuments1_03-06-2013_1114-50.mp3
1,3/6/2013,11:20:24 AM,file:\C:AlertLog1_03-06-2013_1120-24.mp3
1,3/6/2013,11:20:39 AM,file:\C:AlertLog1_03-06-2013_1120-24.mp3

The part I am trying to grab is file:\C:AlertLog1_03-06-2013_1120-24.mp3 – preferably getting rid of the file:\ part.

Sorry that I do not have any code to show, just have a few hours of experience with Node.js and cannot seem to find any docs on how to accomplish something like this. Any help would be appreciated. Thanks!

Solution :

Regular file

Read the file like a regular file, split the file contents into lines, take the last line, split by a comma and take the last part.

var fs = require('fs'); // file system module

fs.readFile('/path/to/file.csv', 'utf-8', function(err, data) {
    if (err) throw err;

    var lines = data.trim().split('n');
    var lastLine = lines.slice(-1)[0];

    var fields = lastLine.split(',');
    var audioFile = fields.slice(-1)[0].replace('file:\\', '');

    console.log(audioFile);
});

File System module documentation

CSV parser

You can also use the node-csv-parser module.

var fs = require('fs');
var csv = require('csv');

csv()
 .from.stream(fs.createReadStream('/path/to/file.csv'))
 .to.array(function(data, count) {
    var lastLine = data.slice(-1)[0];
    var audioFile = lastLine.slice(-1)[0].replace('file:\\', '');
    console.log(audioFile);
  });

I did it in by reading the file backwards until last line was read:

var fs = require('fs');
var path = require('path');
var fileName = path.join(__dirname, 'test.txt');
var readLastLine = function(name, callback) {
    fs.stat(name, function(err, stat) {
        fs.open(name, 'r', function(err, fd) {
            if(err) throw err;
            var i = 0;
            var line = '';
            var readPrevious = function(buf) {
                fs.read(fd, buf, 0, buf.length, stat.size-buf.length-i, function(err, bytesRead, buffer) {
                    if(err) throw err;
                    line = String.fromCharCode(buffer[0]) + line;
                    if (buffer[0] === 0x0a) { //0x0a == 'n'
                        callback(line);
                    } else {
                        i++;
                        readPrevious(new Buffer(1));
                    }
                });
            }
            readPrevious(new Buffer(1));
        });
    });
}
readLastLine(fileName, function(line) {
    console.log(line);
});

Leave a Reply

Your email address will not be published. Required fields are marked *