使用缓冲流以在多个文本文件中搜索字符串&还可以在nodejs中找到行号 [英] using buffer& streams to search for string in multiple text file & also to find line number in nodejs

查看:67
本文介绍了使用缓冲流以在多个文本文件中搜索字符串&还可以在nodejs中找到行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我在多个文件中搜索字符串,我需要使用缓冲区& amp;打印带有文件名的特定字符串的行号.node.js中的流概念.

please help me to search a string across multiple files, I need to print the line number of that particular string with filename using buffer & streams concept in node.js.

例如:

有5个文本文件,并且在第3个文件的第10和15行中有"hello"字符串.第5个文件的第50行中的相同hello字符串.现在我需要打印文件名3的行号以及搜索到的字符串"hello"的行号与第5个文件相同.

there are 5 text files and there is " hello " string in 10 and 15th line of the 3rd file. same hello string in the 50th line of the 5th file. now I need to print line number of file name 3 with the line number of that searched string "hello" same as for the 5th file.

帮助我以node.js中的缓冲区概念编写该程序

help me to write this program in buffer concept in node.js

推荐答案

const readline = require("readline");
const fs = require("fs");

// Start methods implementation
const beginSearch = (readStream, filePath, queries) => {
  let lineCount = 0;
  let matches = new Map();
  queries.forEach(query => matches.set(query, []));

  return new Promise((resolve, reject) => {
    readStream.on("line", line => {
      lineCount++;
      for (query of matches.keys()) {
        if (searchForTerm(line, query))
          matches.set(query, [...matches.get(query), lineCount]);
      }
    });

    readStream.on("close", () => resolve({
      filePath,
      matches
    }));
  });
};
const searchForTerm = (line, query) => line.match(query);

const createLineInterfaces = filePaths =>
  filePaths.map(filePath => {
    const readStream = readline.createInterface({
      input: fs.createReadStream(filePath),
      crlfDelay: Infinity
    });
    return {
      filePath,
      readStream
    };
  });
// End methods implementation


// Start main function

const filesToSearch = ["sample.txt", "sample2.txt"];
const queriesToSeatch = ["hello"];
let searchProms = createLineInterfaces(filesToSearch).map(
  ({
    readStream,
    filePath
  }) =>
  beginSearch(readStream, filePath, queriesToSeatch)
);

Promise.all(searchProms).then(searchResults =>
  searchResults.forEach(result => console.log(result))
);
// End main function

一些解释

我正在使用 readline 模块将每个文件分成几行.请记住,整个实现都是通过流进行的.然后,我将侦听器附加到 line 事件,并在每一行中搜索特定查询.搜索方法是一个简单的正则表达式.如果需要,可以使用模糊搜索方法.然后将匹配的行保存在 Map 中,其中的键是查询,并为查询找到的 lineNumbers 数组赋值.

I am using the readline module to split each file into lines. Keep in mind the whole implementation is with streams. Then i am attaching a listener to the line event and I am searching each line for a specific query. The search method is a simple regexp. You could use a fuzzy search method if you want. Then the matched lines are saved in a Map which keys are the queries and values the arrays of lineNumbers that the query has found.

我假设您熟悉 stream 概念,并且了解 ES6 内容.

I am assuming that you are familiar with the stream concept and you know about ES6 stuff.

这篇关于使用缓冲流以在多个文本文件中搜索字符串&还可以在nodejs中找到行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆