英文:
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('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);
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论