Skip to content

Instantly share code, notes, and snippets.

@CodingBobby
Created May 23, 2019 21:54
Show Gist options
  • Save CodingBobby/fb79bdb2b32e6eef1c6d4c2e38878a0c to your computer and use it in GitHub Desktop.
Save CodingBobby/fb79bdb2b32e6eef1c6d4c2e38878a0c to your computer and use it in GitHub Desktop.
takes file name where you pasted an excel table, reformats it and saves it to a new .csv file
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME')
process.exit(1)
}
const fs = require('fs')
let filename = process.argv[2]
fs.readFile(filename, 'utf8', function (err, data) {
if (err) throw err
// just showing what we got
console.log('Reading: ' + filename + '\n')
// to this, the final stuff will be added
let csv = ''
// split data into lines, replace colons with dots and split the line into data columns at tabbings, put it together with new separator and end the line with a new EOL
data.split('\n').forEach(l => {
csv += l.replace(',', '.').split('\t').join(';').concat('\n')
})
// print to console in case you want to copy & paste
console.log(csv)
// here we first put the new filename together by removing the current file ending and adding the new .csv to the original naming, then we save it to the current directory
let name = filename.slice(0, filename.lastIndexOf('.')).concat('.csv')
fs.writeFile(name, csv, function (err) {
if (err) return console.error(err)
console.log('Saved: ' + name)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment