Skip to content

Instantly share code, notes, and snippets.

@callerobertsson
Created May 13, 2013 15:53
Show Gist options
  • Save callerobertsson/5569353 to your computer and use it in GitHub Desktop.
Save callerobertsson/5569353 to your computer and use it in GitHub Desktop.
Javascript (NodeJS): A simple script for extracting parts in a textfile between two regex matching lines. #snippet
// Filter File
// a simple script for extracting parts in a textfile between two lines.
// by Calle Robertsson, [email protected], 2013.
var fs = require('fs');
var filename = process.argv[2];
if (!filename) {
throw new Error('Missing command line argument for file name.');
}
var StartRegex = /^GRANT.*/;
var EndRegex = /^\s*$/;
var IncludeEnd = false;
var AddEmptyRow = true;
fs.readFile(filename, 'utf8', function (err, data) {
if (err) throw err;
parse(data);
});
function parse(text) {
var rows = text.split('\n');
var outRow = false;
for (var i in rows) {
var row = rows[i];
if (StartRegex.exec(row)) {
if (AddEmptyRow) {
console.log('');
}
outRow = true;
}
else if (EndRegex.exec(row)) {
if (IncludeEnd) {
console.log(row);
}
outRow = false;
}
if (outRow) {
console.log(row);
}
}
console.log('');
}
// eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment