Alta istar Alta istar - 3 months ago 16
Node.js Question

How to remove one line from a txt file

I have the following text file ("test.txt") that I want to manipulate in node.js:

world
food


I want to remove the first line so that
food
becomes the first line instead. How can I do that?

Answer
var fs = require('fs')
fs.readFile(filename, 'utf8', function(err, data)
{
    if (err)
    {
        // check and handle err
    }
    var linesExceptFirst = data.split('\n').slice(1).join('\n');
    fs.writeFile(filename, linesExceptFirst);
});