Skip to content

Instantly share code, notes, and snippets.

@isabek
Last active August 29, 2015 14:14
Show Gist options
  • Save isabek/e3f09b403b67c25928d2 to your computer and use it in GitHub Desktop.
Save isabek/e3f09b403b67c25928d2 to your computer and use it in GitHub Desktop.
/** First argument should be a filename, second head, third tail **/
var fs = require("fs");
var split = require("split");
var through = require("through");
var concat = require("concat-stream");
var filename = process.argv[2];
var head = Math.abs(process.argv[3] || 1);
var tail = Math.abs(process.argv[4] || 1);
var tailLimit = head - tail;
var headCounter = 1;
var tailCounter = 1;
var headlines = through(function (line) {
if (headCounter > head) return;
this.queue(line);
headCounter++;
});
var tailLines = through(function (line) {
if (tailLimit < tailCounter) {
this.queue(line + "\n");
}
tailCounter++;
});
fs.createReadStream(filename)
.pipe(split())
.pipe(headlines)
.pipe(tailLines)
.pipe(concat(function (data) {
console.log(data);
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment