Skip to content Skip to sidebar Skip to footer

Removing A Line From A Txt File

How can I delete a row from a txt file without empty lines (Example) (Example2).For example, I have a file: Hello Hello2 String After the deletion, the file will be: Hello Hello2

Solution 1:

The problem you're having is that you cannot only remove the text as there are carriage return and line feed characters to consider \r\n.

If you know the \r\n characters always follow the text you can do this:

var fs = require('fs');

let strToRemove = 'hello2\r\n';
let fileName = 'test.txt';
fs.readFile(fileName, 'utf8', function(err, data){
    let toRemove = data.replace(strToRemove,'');
    fs.writeFile(fileName, toRemove);
});

Post a Comment for "Removing A Line From A Txt File"