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 ?