Skip to content

Instantly share code, notes, and snippets.

@loganwoolf
Created February 6, 2025 21:54
Show Gist options
  • Save loganwoolf/8d9612518fae2458da3a38fe5ebf34a6 to your computer and use it in GitHub Desktop.
Save loganwoolf/8d9612518fae2458da3a38fe5ebf34a6 to your computer and use it in GitHub Desktop.
Node read/write stream
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