Created
July 14, 2022 00:16
-
-
Save munanadi/9d0f60f665d0d965e3c508a45b018856 to your computer and use it in GitHub Desktop.
Script to manipulate line by line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // TO swithc amt0 and amt1 that got switched up when creating this file | |
| const fs = require('fs'); | |
| const readline = require('readline'); | |
| const { EOL } = require('os'); | |
| async function processLineByLine() { | |
| const fileStream = fs.createReadStream(__dirname + '/manipulate.txt'); | |
| const writeStream = fs.createWriteStream(__dirname + '/good_manipulate.txt', { | |
| encoding: 'utf8', | |
| }); | |
| const rl = readline.createInterface({ | |
| input: fileStream, | |
| crlfDelay: Infinity, | |
| }); | |
| // Note: we use the crlfDelay option to recognize all instances of CR LF | |
| // ('\r\n') 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`. | |
| // console.log(`${line.split(',')[0]}`); | |
| // Given a line of CSV | |
| const [hash, blocktime, pool, sender, amt1, amt0] = line.split(','); | |
| // Re-arrange the CSV as required and write it back to a file | |
| const str = `${hash},${blocktime},${pool},${sender},${amt0},${amt1}`; | |
| writeStream.write(`${str}${EOL}`); | |
| } | |
| } | |
| processLineByLine(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment