Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Last active October 8, 2015 11:28
Show Gist options
  • Save rjcorwin/3325639 to your computer and use it in GitHub Desktop.
Save rjcorwin/3325639 to your computer and use it in GitHub Desktop.
Transform text in a file into another file, line by line. Handy for cleaning up CSV files. A node.js example.
var BufferedReader = require ("buffered-reader")
var BufferedWriter = require ("buffered-writer")
var $ = require ("jquery")
var targetFile = "target.txt"
var sourceFile = "source.txt"
var linesArray = []
var transform = function (line) {
// your transform here
return line;
}
new BufferedReader (sourceFile, { encoding: "utf8" })
.on ("error", function (error){
console.log ("error: " + error)
})
.on ("line", function (line){
linesArray.push(line)
})
.on ("end", function (){
var target = new BufferedWriter (targetFile)
.on ("error", function (error){
console.log (error)
})
$.each(linesArray, function(key, line) {
line = transform(line)
target.write (line + " \n")
})
target.close ()
})
.read ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment