Created
October 18, 2013 09:05
-
-
Save e7h4n/7038750 to your computer and use it in GitHub Desktop.
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 input = process.argv[2]; | |
var keyword = process.argv[3]; | |
var output = process.argv[4]; | |
var fs = require('fs'); | |
var rs = fs.createReadStream(input, { | |
flags: 'r', | |
encoding: 'utf-8' | |
}); | |
var ws = fs.createWriteStream(output, { | |
flags: 'w+', | |
encoding: 'utf-8' | |
}); | |
var matched = false; | |
var CRLF = '\r\n'; | |
var prevline = ''; | |
rs.on('data', function (chunk) { | |
var lines = chunk.split(CRLF); | |
lines[0] = prevline + lines[0]; | |
if (chunk.substr(chunk.length - 2) !== CRLF) { | |
prevline = lines.pop(); | |
} else { | |
prevline = ''; | |
} | |
lines.forEach(function (line) { | |
if (line.indexOf(keyword) !== -1) { | |
ws.write(line + CRLF); | |
} | |
}); | |
}); | |
rs.on('end', function () { | |
ws.end(function () { | |
console.log('done'); | |
}); | |
}); |
用 child_process 调用 cat grep 更快 我无聊写了个 哈哈
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
点赞!