【问题标题】:Question about Find string and delete line - Node.JS关于查找字符串和删除行的问题 - Node.JS
【发布时间】:2020-08-07 18:27:36
【问题描述】:

Find string and delete line - Node.JS

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');
  });

});

此链接说明如何查找字符串并删除行,但当时只删除一个 user1。我与 user1 有很多行,如何删除所有行:

john
doe
some keyword
user1
last word
user1
user1

反之亦然。如何删除所有行并只保留 user1 行?

【问题讨论】:

    标签: node.js readlines


    【解决方案1】:

    我会使用Array.filter() 函数。

    基本上,您在数组上调用filter(),并定义一个回调函数来检查该数组的每一项。

    如果检查函数为特定项目返回true,请保留该项目 - 将其放入新数组 如果检查函数返回false,则不要将该项放入新数组中

    因此,在您的情况下,一旦您将所有行读入一个数组(代码中的第 6 行),请使用 filter 函数:

    // Delete all instances of user1
    let newDataArray = dataArray.filter(line => line !== "user1")
    
    // Delete everything except user1
    let newDataArray = dataArray.filter(line => line === "user1")
    
    // Delete any lines that have the text 'user1' somewhere inside them
    let newDataArray = dataArray.filter(line => !line.includes("user1"))
    
    

    然后,就像您在代码中所做的那样,在 newDataArray() 上使用 join() 函数并写入文件。


    要重写你的代码,

    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
    
      // Delete all instances of user1
      let newDataArray = dataArray.filter(line => line !== searchKeyword)
    
      // UPDATE FILE WITH NEW DATA
      const updatedData = newDataArray.join('\n');
    
      fs.writeFile('shuffle.txt', updatedData, (err) => {
        if (err) throw err;
        console.log ('Successfully updated the file data');
      });
    
    });
    

    【讨论】:

    • 谢谢 我试图修改脚本但我完全迷路了。究竟在哪里添加“let newDataArray = dataArray.filter(line => line !== "user1")” 以及从原始脚本中取出什么?
    • 我重写了你给的代码,希望现在清楚
    • 1) 它适用于: // 删除 user1 的所有实例 let newDataArray = dataArray.filter(line => line !== searchKeyword)
    • 2) 它适用于: // 删除除 user1 之外的所有内容 // let newDataArray = dataArray.filter(line => line === searchKeyword)
    • 3) 它不适用于 :// 删除其中包含文本 'user1' 的任何行 let newDataArray = dataArray.filter(line => line.includes (searchKeyword))结果就像数字 2)删除除 user1 之外的所有内容
    猜你喜欢
    • 2019-03-27
    • 1970-01-01
    • 2021-07-11
    • 2020-09-27
    • 1970-01-01
    • 2016-02-03
    • 2011-12-20
    • 2020-10-14
    • 2014-03-20
    相关资源
    最近更新 更多