Skip to content

Instantly share code, notes, and snippets.

@yuanchuan
Created May 21, 2012 06:20
Show Gist options
  • Save yuanchuan/2760768 to your computer and use it in GitHub Desktop.
Save yuanchuan/2760768 to your computer and use it in GitHub Desktop.
filter log file with nodejs for fun
#!/usr/bin/env node
var fs = require('fs')
, input = 'worklog.in'
, output = 'worklog.out';
var spliters = {
block: 'Revision'
, field: [
{ name: 'date', head: 'Date: ', tail: '\n' }
, { name: 'msg', head: 'Message:', tail: '----' }
]
}
function readAndWrite(input, output, process) {
fs.readFile(input, 'utf-8', function(err, content) {
if (err) throw err;
fs.writeFile(output, process(content), function(err){
if (err) throw err;
else console.log('done.');
});
})
}
readAndWrite(input, output, function(content) {
return content
.split(spliters.block)
.map(function(block) {
return spliters.field.map(function(spliter) {
var temp = block.split(spliter.head);
return temp.length
&& (temp = temp[1])
&& (temp = temp.split(spliter.tail)[0])
&& temp.replace(/\n+/g, ' ')
|| '';
}).join('');
}).reverse().join('\n');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment