Skip to content

Instantly share code, notes, and snippets.

@nilsnh
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilsnh/1d9c9a72b5aca8205cbb to your computer and use it in GitHub Desktop.
Save nilsnh/1d9c9a72b5aca8205cbb to your computer and use it in GitHub Desktop.
Compare files and create a new file containing the missing lines. Used like: node File1 File2 FileWithLinesMissingInFile2.txt
var fs = require('fs');
var oldReport = [];
var newReport = [];
var missingLines = [];
fs.readFile(process.argv[2], {encoding: "utf8"}, function (err, data) {
if (err) throw err;
oldReport = data.split('\n');
readNewReportFile()
});
function readNewReportFile() {
fs.readFile(process.argv[3], {encoding: "utf8"}, function (err, data) {
if (err) throw err;
newReport = data.split('\n');
runComparison();
});
}
function runComparison() {
console.log("comparing files");
oldReport.forEach(function(oldLine){
if(newReport.indexOf(oldLine) == -1) missingLines.push(oldLine);
});
appendToFile(missingLines);
}
function appendToFile (data) {
fs.writeFile(process.argv[4], data.join('\n'), {encoding: 'utf8'}, function (err) {
if (err) throw err;
console.log('Appended missing lines to file!');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment