Created
May 21, 2012 06:20
-
-
Save yuanchuan/2760768 to your computer and use it in GitHub Desktop.
filter log file with nodejs for fun
This file contains hidden or 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
#!/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