Skip to content

Instantly share code, notes, and snippets.

@e7h4n
Created October 18, 2013 09:05
Show Gist options
  • Save e7h4n/7038750 to your computer and use it in GitHub Desktop.
Save e7h4n/7038750 to your computer and use it in GitHub Desktop.
#!/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');
});
});
@finscn
Copy link

finscn commented Oct 18, 2013

点赞!

@finscn
Copy link

finscn commented Oct 18, 2013

用 child_process 调用 cat grep 更快 我无聊写了个 哈哈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment