3

How to read file, search for string and delete line in node.js? I have tried

var fs = require('fs')
fs.readFile('shuffle.txt', function read(err, data) {
if (err) {
throw err;
}

lastIndex = function(){
for (var i = data_array.length - 1; i > -1; i--)
if (data_array[i].match('user1'))
return i;
}()

delete data_array[lastIndex];

});
john heroi
  • 47
  • 2
  • 12
  • A number of problems. Where is 'data_array' getting initialized? Also, the data from your file is only available in the callback function. lastIndex is a function, not a value, so delete data_array[lastIndex] is probably not what you are looking for. Break the problem down and test it a piece at a time. – Jim B. Oct 22 '18 at 23:41
  • After deleting the line, do you have to update the content of the file? – Shams Nahid Oct 23 '18 at 01:05
  • Yes, thanks alot for your answer. Is there any solution in javascript for this as well? – john heroi Oct 23 '18 at 09:18
  • I wrote the answer in javascript. – Shams Nahid Oct 23 '18 at 10:07
  • Thanks for your help! If its not too much could you help me here: https://stackoverflow.com/questions/52935965/unirest-setting-proxy/52937907#52937907 (Btw I just meant client side javascript as this is client side node.js?) – john heroi Oct 23 '18 at 13:19

1 Answers1

3

Let's say we have a text file, shuffle.txt contains the following content

john
doe
user1 
some keyword
last word

Now we read the shuffle.txt file and then search for 'user1' keyword. If any line contains the 'user1', then we will remove the line.

var fs = require('fs')
fs.readFile('shuffle.txt', {encoding: 'utf-8'}, function(err, data) {
    if (err) throw error;

    let dataArray = data.split('\n'); // convert file data in an array
    const searchKeyword = 'user1'; // we are looking for a line, contains, key word 'user1' in the file
    let lastIndex = -1; // let say, we have not found the keyword

    for (let index=0; index<dataArray.length; index++) {
        if (dataArray[index].includes(searchKeyword)) { // check if a line contains the 'user1' keyword
            lastIndex = index; // found a line includes a 'user1' keyword
            break; 
        }
    }

    dataArray.splice(lastIndex, 1); // remove the keyword 'user1' from the data Array

    // UPDATE FILE WITH NEW DATA
    // IN CASE YOU WANT TO UPDATE THE CONTENT IN YOUR FILE
    // THIS WILL REMOVE THE LINE CONTAINS 'user1' IN YOUR shuffle.txt FILE
    const updatedData = dataArray.join('\n');
    fs.writeFile('shuffle.txt', updatedData, (err) => {
        if (err) throw err;
        console.log ('Successfully updated the file data');
    });

});

Here, if a line contains 'user1' keyword, we are removing the entire line. The new shuffle.txt file will be no longer contains a line with 'user1' keyword. The updated shuffle.txt file looks like

john
doe
some keyword
last word

For more information check the doc.

Shams Nahid
  • 6,239
  • 8
  • 28
  • 39
  • Great answer! What would be the logic to use to update a large text file when using fs.writeStream? See my problem here https://stackoverflow.com/questions/71191431/how-to-delete-lines-of-text-from-file-with-createwritestream-with-node-js – Grogu Feb 20 '22 at 21:26