console.log( [...require('fs') .readFileSync('/dev/stdin', 'utf8')] .reverse() .map((v, i) => i % 3 ? v : `${v},`) .reverse() .join `` .slice(0, -1) );