LoginSignup
4
9

More than 5 years have passed since last update.

Node.jsでファイルを一行ずつ読み込んで、加工して書き出すスクリプト書いた

Posted at

カンマ区切りのデータを、ちょろっと修正したい。そんなスクリプト。

割と使うので備忘録的に置いておきます。

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

const reader = readline.createInterface({ input: fs.createReadStream('./aaa.txt', 'utf8') });
const writer = fs.createWriteStream('./out.csv');

function aaa(line) {
  const arr = line.split(',');
  console.log(arr[0], arr[1], arr[2]);
  writer.write(`${arr[0]},${arr[1]},${arr[2]}\r\n`);
}

reader.on('line', aaa);
4
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9