Last active
October 8, 2015 11:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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