0

I have a Hex file which consists of around 3600 lines. Now I have a one-word 1000 in the file need to search that and read the exact above line of that text. For example:

1:2007200040040020A006000000050020A806000010050020B0060000101E0
2:2007400000020020C0060000EC010020C7060000F0010020CE060000F8010
3:04076000040200206F
4:20**1000**00B41200001810000015AB0110FDB301106DC00110FDAE011099B501
5:08102000000000002D0000009B
6:20102800B01200003C100000B1C00110A9BF011067C0011069B801
7:041048003000000074

Now in the above data 1000 is located at line number 4 and fetch Line number 3 data from that search operation.

I am using nodejs FS library.

Below is my code so far:

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===');
})

Thank you.

Akash M
  • 488
  • 3
  • 6
  • 18
  • You could use the index of [forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) to get the line number: `lines.forEach(linebyline =>` to `lines.forEach((linebyline, index) =>` – Reyno May 11 '21 at 11:36
  • Does this answer your question? [Read a file one line at a time in node.js?](https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js) – Heretic Monkey May 11 '21 at 11:59

2 Answers2

0
const fs = require('fs');
const readline = require('readline');

async function findPreviousLine(filename, searchString) {
    const fileStream = fs.createReadStream(filename);
  
    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });

    let previousLine = '';
  
    for await (const line of rl) {
        if (line.includes(searchString)) {
            console.log(previousLine)
            return;
        }

        previousLine = line;
    }
}

findPreviousLine('data.txt', '1000');

See the code in this answer for line by line reading.

boranseckin
  • 335
  • 2
  • 12
0

Here is the working example

const fs = require('fs');
const parseTxt = async (csvFile) => {
  let result=[];    
  const data = await fs.readFileSync(csvFile);
  const str = data.toString();
  const lines = str.split('\n');
  var total_lines = lines.length;

  lines.map(line => {
      if(!line) {return null}
      result.push(Number(line))
  });

  lines.forEach(linebyline => {
      var search1000 = linebyline.indexOf('1000');
      if(search1000 > -1){
          // 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===');
})
Shubham
  • 206
  • 1
  • 9