0

I am trying to read the txt file and push each word into array using Node.js but as per my code its not happening in proper format. I am explaining my txt file below.

CHANGES:

CHANGES
==============================
v2.2.3              11/06/2021

Changes:
- USxxxx abcd
- DExxxx xyz

===============================
v2.2.2               01/06/2021

Changes:
- USxxxx abcd
- DExxxx xyz

===============================
v2.2.1               20/05/2021

Changes:
- USxxxx abcd
- DExxxx xyz

===============================

My code is given below.

reader = fs.createReadStream('../../../CHANGES');
 
// Read and display the file data on console
let arr = [];
reader.on('data', function (chunk) {
  arr.push(chunk.toString());
  //console.log('chunk', chunk.toString());
  console.log('chunk', arr);
});

Here I want to push each word into array. but the it gives the output like below.

chunk [
  'CHANGES\n' +
    '==============================\n' +
    'v2.2.3              11/06/2021\n' +
    '\n' +
    'Changes:\n' +
    '- USxxxx abcd\n' +
    '- DExxxx xyz\n' +
    '\n' +
    '===============================\n' +
    'v2.2.2               01/06/2021\n' +
    '\n' +
    'Changes:\n' +
    '- USxxxx abcd\n' +
    '- DExxxx xyz\n' +
    '\n' +
    '===============================\n' +
    'v2.2.1               20/05/2021\n' +
    '\n' +
    'Changes:\n' +
    '- USxxxx abcd\n' +
    '- DExxxx xyz\n' +
    '\n' +
    '==============================='
]

My expected format should be like below.

[
  'CHANGES' ,
    'v2.2.3',
    '11/06/2021',
    'Changes'
    '- USxxxx abcd',
    '- DExxxx xyz',
    'v2.2.2 ',
    '01/06/2021'
    'Changes:',
    '- USxxxx abcd',
    '- DExxxx xyz',
    'v2.2.1',
    '20/05/2021',
    'Changes:',
    '- USxxxx abcd',
    '- DExxxx xyz'
]

Here /n and + are adding but I need to read each word and push into array. Can anybody please suggest me how to solve this ?

subhra_user
  • 439
  • 5
  • 19
  • What would be the words in the text file? Things like "CHANGES", "USxxxx" or something else? – arfat Jun 17 '21 at 06:47
  • @arfat, yes, like `CHANGES, v2.2.3, 11/06/2021 etc...` – subhra_user Jun 17 '21 at 06:51
  • You're pushing whatever chunks of data the readstream sends you. If you want to be pushing individual lines into the array, then you need to parse the chunks into lines and you need to be warry of line boundaries that can span chunks. You may want to use one of the linereader modules in NPM that does this for you. – jfriend00 Jun 17 '21 at 06:52
  • @jfriend00, Can you please post your idea in details ? – subhra_user Jun 17 '21 at 06:54
  • Please show in your question an example of what exactly the desired array should contain. – jfriend00 Jun 17 '21 at 07:00
  • 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) – Keith Jun 17 '21 at 07:02
  • @jfriend00, please check my updated post. – subhra_user Jun 17 '21 at 07:08
  • Just use one of the linereader modules in NPM to break the stream into lines for you and then you will have to write custom code to filter out the lines you don't want and break up the lines that you want split. – jfriend00 Jun 17 '21 at 07:22

1 Answers1

1

You should be able to use the Readline module for this, creating a readline interface from the file stream, for example:

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

const reader = readline.createInterface({
    input: fs.createReadStream('../../../CHANGES'),
});

// Skip empty lines and lines like '===='
function lineFilter(line) {
    return (line.length > 0) && (!line.includes('=================='));
}

async function readLines() {
    let arr = [];
    for await (let line of reader) {
        if (lineFilter(line)) {
            // Split version lines.
            if (/v\d\.\d\.\d/.test(line)) {
                line.split('           ').forEach(f => arr.push(f.trim()));
            } else { 
                arr.push(line);
            }
        }
    }
    console.log("Result:", JSON.stringify(arr, null, 2));
}

readLines();

We'll see a result like below:

Result: [
  "CHANGES",
  "v2.2.3",
  "11/06/2021",
  "Changes:",
  "- USxxxx abcd",
  "- DExxxx xyz",
  "v2.2.2",
  "01/06/2021",
  "Changes:",
  "- USxxxx abcd",
  "- DExxxx xyz",
  "v2.2.1",
  "20/05/2021",
  "Changes:",
  "- USxxxx abcd",
  "- DExxxx xyz"
]
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40