使用readline node.js搜索文本文件 [英] Searching text file with readline node.js
问题描述
请考虑以下代码.我想要做的是从 createStream
函数中的 names.txt
中一个接一个地选择名称,然后从其中使用调用
作为参数,并逐行搜索 FileSearch
函数将nameToSearch list.txt
.因此,一个名称并逐行搜索 list.txt
,另一名称并逐行搜索 list.txt
,依此类推...
Please consider below code. What I want to do is to pick name from names.txt
in createStream
function one by one and from there call FileSearch
function with nameToSearch
as parameter and search list.txt
line by line. So one name and search list.txt
line by line, another name and search list.txt
line by line and so on...
list.txt
是一个大文件.如果找到匹配项,则输出搜索字符串和行号.我以为它会放一个名字和行号,然后再移到第二个名字,依此类推,但是它是
list.txt
,is a large file. If finds a match, output the search string and line number. I thought it'll out put one name and line numbers and then move on to second name and so on but it is
-
跳过行.我手动搜索了一些名称,输出中没有相应的行号)
Skipping rows. I searched some names manually and corresponding line numbers are not there in output)
混合名称.我可以在输出的初始部分看到名称,然后在输出的其他地方看到.
Mixing names. I can see names in the initial part of the output and then somewhere else in the output.
代码:
var fs = require('fs')
var readline = require('readline');
var nameToSearch
var createStream = function (){
var lineNumber=0;
var ended = false;
var rlName = readline.createInterface({
input : fs.createReadStream('./names.txt'),
output: process.stdout,
terminal: false
})
rlName.on('line',function(lineInNameFile){
++lineNumber;
nameToSearch=lowercase(lineInNameFile);
FileSearch(nameToSearch);
})
}
var lowercase=function(str){
return String(str).toLowerCase();
}
var FileSearch = function (searchString){
var listlineNumber=0;
var rlList = readline.createInterface({
input : fs.createReadStream('./list-2.txt'),
output: process.stdout,
terminal: false
})
rlList.on('line',function(lineInListFile){
++listlineNumber;
if (lineInListFile===searchString){
console.log( searchString+ ":" + listlineNumber);
}
})
}
var runSearch = createStream();
exports.createStream = createStream;
names.txt
看起来像这样
OLIVER
CHARLOTTE
LIAM
AMELIA
.
.
和 list.txt
看起来像这样,两个文件中每行基本上一个字
and list.txt
looks like this, basically one word per line in both files
a
aah
aahed
aahing
aback
abacus
abacuses
abaft
abalone
abalones
.
.
我放在控制台上的输出是这样
My out put on console is something like this
grayson:2322
emmett:3756
emmett:6399
lily:3739
lily:6340
.
.
我不正确使用 readline
模块吗?
编辑:在调查了更多内容并进行搜索后,发现它可能与阅读最后一行有关.它没有读取 list.txt
的最后一行.我不确定为什么
upon looking into more and searching found that it might have to do something with reading last line. Its not reading the last line of list.txt
. I am not sure why
推荐答案
我设法通过使用逐行模块来解决此问题,现在输出与预期的一样.这是代码
I manage to resolve the issue by using line-by-line module and now the output is exact as expected. heres the code
var nameToSearch
//Function to retrieve names
var createStream = function (){
var lineNumber=0;
var nameLine = require('line-by-line')
lrName = new nameLine('./names.txt');
//lrName = new nameLine('./names-testFile.txt');
var ended = false;
lrName.on('line', function (lineInNameFile) {
++lineNumber;
nameToSearch=lowercase(lineInNameFile);
FileSearch(nameToSearch);
});
}
var lowercase=function(str){
return String(str).toLowerCase();
}
//Function to search in list file
var FileSearch = function (searchString){
var LineByLineReader = require('line-by-line'),
lr = new LineByLineReader('./list.txt');
//lr = new LineByLineReader('./list-testFile.txt');
var listlineNumber=0
var arr = []
lr.on('line', function (lineInListFile) {
++listlineNumber;
if (lineInListFile===searchString){
arr.push(listlineNumber);
}
});
lr.on('end', function () {
console.log( searchString+ ":" + arr);
});
}
var runSearch = createStream();
exports.createStream = createStream;
这篇关于使用readline node.js搜索文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!