在多个文件中搜索关键词,并将带有前一行和后一行的结果返回给我。

huangapple go评论60阅读模式
英文:

Search for a keyword in multiple files and return me the result with previous and following line

问题

I'm using Node.js, 我正在使用 Node.js,
I want to search for a keyword in multiple .txt files, 我想在多个 .txt 文件中搜索关键词,
and then that return me the previous line and the following one. 然后返回给我前一行和后一行。

I'm expecting something like this: 我期望得到类似这样的结果:

Cars
Scooters
Skates
Airplanes
Ships
Trucks
Bikes

Then I search for the word using some Node.js package, 然后我使用某个 Node.js 包搜索这个词,
in this example is ship. 在这个示例中是 "ship"。
So the package returns to me the line that is upper and down. 所以该包返回给我上下的行。
For example, something like this: 例如,类似这样的结果:

{"result":
"Airplanes
Ships
Trucks
"}
英文:

I'm using Node JS, I want to search for a keyword in multiple .txt files, and then that return me the previous line and the following one.

Im expecting something like this:

Cars
Scooters
Skates
Airplanes
Ships
Trucks
Bikes

Then I search for the word using some nodejs package, in this example is ship. So the package return to me the line that is upper and down. For example something like this:

{"result":"
Airplanes
Ships
Trucks
"}

答案1

得分: 0

你可以使用内置的 fs 模块来读取文本文件的内容。

https://www.w3schools.com/nodejs/nodejs_filesystem.asp

const fs = require('fs');

function searchKeywordInFiles(keyword, files) {
  const results = [];

  files.forEach(file => {
    const data = fs.readFileSync(file, 'utf8');
    const lines = data.split('\n');

    for (let i = 0; i < lines.length; i++) {
      if (lines[i].includes(keyword)) {
        const previousLine = lines[i - 1] || '';
        const nextLine = lines[i + 1] || '';

        results.push({
          result: `${previousLine}\n${lines[i]}\n${nextLine}`
        });
      }
    }
  });

  return results;
}

// Example usage
const keyword = 'ship';
const files = ['file1.txt', 'file2.txt', 'file3.txt']; // Replace with your actual file names

const searchResults = searchKeywordInFiles(keyword, files);
console.log(searchResults);

在这个示例中,searchKeywordInFiles 函数接受关键字和文件数组作为参数。它使用 fs.readFileSync 读取每个文件,将内容分割成行,并遍历每一行以检查是否包含关键字。

如果一行包含关键字,它会检索前一行(如果有的话)和后一行(如果有的话),然后将它们添加到结果数组中。最后,它返回搜索结果数组。

英文:

You can use the built-in fs module to read the contents of the text files

https://www.w3schools.com/nodejs/nodejs_filesystem.asp

const fs = require(&#39;fs&#39;);

function searchKeywordInFiles(keyword, files) {
  const results = [];

  files.forEach(file =&gt; {
    const data = fs.readFileSync(file, &#39;utf8&#39;);
    const lines = data.split(&#39;\n&#39;);

    for (let i = 0; i &lt; lines.length; i++) {
      if (lines[i].includes(keyword)) {
        const previousLine = lines[i - 1] || &#39;&#39;;
        const nextLine = lines[i + 1] || &#39;&#39;;

        results.push({
          result: `${previousLine}\n${lines[i]}\n${nextLine}`
        });
      }
    }
  });

  return results;
}

// Example usage
const keyword = &#39;ship&#39;;
const files = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]; // Replace with your actual file names

const searchResults = searchKeywordInFiles(keyword, files);
console.log(searchResults);

In this example, the searchKeywordInFiles function takes a keyword and an array of files as parameters. It reads each file using fs.readFileSync, splits the content into lines, and iterates over each line to check if it contains the keyword.

If a line contains the keyword, it retrieves the previous line (if available) and the next line (if available) and adds them to the results array. Finally, it returns the array of search results.

huangapple
  • 本文由 发表于 2023年5月21日 12:10:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定