Last active
August 29, 2015 14:04
-
-
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
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 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