NodeJs逐行和下一行读取



我使用Node readline模块逐行读取文件,同时检查每行中的某些条件。但是,这些文件的换行符不一致。有时,该行会中断到下一行,导致if条件无法捕获。有没有办法在当前行中检查下一行?非常感谢您的帮助。谢谢

const fs = require('fs');
const readline = require('readline');
async function processLineByLine() {
const fileStream = fs.createReadStream('input.txt');
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('rn') in input.txt as a single line break.
for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
if(line.includes('abc') && nextline.includes('def')) {

}
}
processLineByLine();

样本输入

单线

<ref href="sample.txt">

双线

<ref 
href="sample.txt">

您可以执行类似的操作

let previousLine;
for await (let line of rl) {
// Each line in input.txt will be successively available here as `line`.
// check if current line fulfils your conditions
// if not and previousLine has value check conditions with the 2 lines 
// if not you store current line into previousLine 
previousLine = line;
}

最新更新