Created
February 6, 2025 21:54
-
-
Save loganwoolf/8d9612518fae2458da3a38fe5ebf34a6 to your computer and use it in GitHub Desktop.
Node read/write stream
This file contains 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
const fs = require('fs'); | |
const readline = require('readline'); | |
const inputFilePath = process.argv[2]; // Change this to your desired input CSV file path | |
const outputFilePath = process.argv[3]; // Change this to your desired output CSV file path | |
const readInterface = readline.createInterface({ | |
input: fs.createReadStream(inputFilePath), | |
output: fs.createWriteStream(outputFilePath), | |
console: false, | |
}); | |
let isFirstLine = true; | |
const outputStream = fs.createWriteStream(outputFilePath); | |
readInterface.on('line', (line) => { | |
if (isFirstLine) { | |
outputStream.write(`${line}\n`); | |
isFirstLine = false; | |
} else if (line.startsWith('Product')) { | |
outputStream.write(`${line}\n`); | |
} | |
}); | |
readInterface.on('close', () => { | |
outputStream.end(); | |
console.log('CSV processing completed.'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment