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 delete the first line so that it is instead food. How can i do this?

+4
source share
1 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);
});
+10
source

Source: https://habr.com/ru/post/1650625/


All Articles