简体   繁体   English

如何使用 node.js 检查文本文件中的重复行?

[英]How can I check for repeating lines in a text file with node.js?

I am creating a node.js program which scans through a log file and outputs information from it to a console.我正在创建一个 node.js 程序,它扫描日志文件并将信息从它输出到控制台。

Sometimes, the log file can contain errors which can repeat basically forever (I'm talking like 20000 times).有时,日志文件可能包含基本上可以永远重复的错误(我说的是 20000 次)。

I need a way to check if any portion of text is repeated multiple times in the file.我需要一种方法来检查文件中是否有任何文本部分重复多次。

Since I don't know what text I'm looking for, I can't use native JS functions, regex, or stuff like that.因为我不知道我在寻找什么文本,所以我不能使用原生 JS 函数、正则表达式或类似的东西。

Does anyone know how I could achieve this without using machine learning?有谁知道我如何在不使用机器学习的情况下实现这一目标?

I have not tried anything yet because I have absolutely no clue how this could be achieved.我还没有尝试过任何东西,因为我完全不知道如何实现这一点。

Break the problem up into multiple steps.将问题分解为多个步骤。 Deal with one step at a time.一次处理一个步骤。 So, for step one, your task is to figure out how to read a file from disk into a variable.因此,对于第一步,您的任务是弄清楚如何将文件从磁盘读取到变量中。 Next step: turn that variable into an array.下一步:将该变量转换为数组。 etc.等等。

You can use an algorithm something like this:你可以使用这样的算法:

  1. Read the log file into memory.将日志文件读入内存。 (If the log file is too large, or if step 2 will be too large, research breaking up this task into multiple parts) (如果日志文件太大,或者如果第 2 步太大,请研究将此任务分解为多个部分)
  2. Turn the log file into an array of discrete pieces of text (therefore, you need to know what separates the discrete pieces of text).将日志文件转换为离散文本片段的数组(因此,您需要知道是什么分隔了离散文本片段)。
  3. Now you need an (empty) output array.现在您需要一个(空)输出数组。
  4. Loop through your input array and, for each array element, check if it is already in the output array.遍历输入数组,对于每个数组元素,检查它是否已经在输出数组中。 If not, add it.如果没有,请添加它。 If yes, do nothing.如果是,什么都不做。

At the end, you will have an output array consisting only of unique log entries.最后,您将拥有一个仅包含唯一日志条目的输出数组。 Write it out to a file.将其写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM