【发布时间】:2021-08-01 18:25:35
【问题描述】:
我有一个包含大约 3600 行的 Hex 文件。现在我在文件中有一个单词 1000 需要搜索并阅读该文本的确切上述行。例如:
1:2007200040040020A006000000050020A806000010050020B0060000101E0
2:2007400000020020C0060000EC010020C7060000F0010020CE060000F8010
3:04076000040200206F
4:20**1000**00B41200001810000015AB0110FDB301106DC00110FDAE011099B501
5:08102000000000002D0000009B
6:20102800B01200003C100000B1C00110A9BF011067C0011069B801
7:041048003000000074
现在在上述数据中,1000 位于第 4 行,并从该搜索操作中获取第 3 行数据。
我正在使用 nodejs FS 库。
以下是我目前的代码:
const parseTxt = async (csvFile) => {
const data = await fs.readFileAsync(csvFile);
const str = data.toString();
const lines = str.split('\r\n');
var total_lines = lines.length;
console.log('Total Lines', total_lines);
lines.map(line => {
if(!line) {return null}
result.push(Number(line))
});
lines.forEach(linebyline => {
var search1000 = linebyline.substring(3, 7);
if(search1000 == '1000'){
// Here I want to check line number of this search and also above line data as well as line number of that above line.
// Also want to Append "DOWNLOAD" text starting of this line data.
}
});
}
parseTxt('file.hex').then(() => {
// console.log(mergeSort({arr: result, count: 0}));
console.log('parseTxt===');
})
谢谢。
【问题讨论】:
-
你可以使用forEach的索引来获取行号:
lines.forEach(linebyline =>到lines.forEach((linebyline, index) => -
这能回答你的问题吗? Read a file one line at a time in node.js?
标签: javascript node.js arrays express