Alta istar Alta istar - 1 year ago 66
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:


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

Answer Source
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);