Last active
August 29, 2015 14:14
-
-
Save isabek/e3f09b403b67c25928d2 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
/** 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