3 ms·
He writes (head -5; tail -5) <data but that's a bit misleading. These don't work. seq 20 | (head -5; tail -5) (head -5; tail -5) < <(seq 20) Bot
by ralph 14y ago
He writes
(head -5; tail -5) <data
but that's a bit misleading. These don't work.
seq 20 | (head -5; tail -5)
(head -5; tail -5) < <(seq 20)
Both giving just the first five lines.
- derekp7 14y agoAre you saying his example doesn't work, or that it can't be extended to your two examples? Because when working on a file it works fine -- just not through a pipe (which both your examples are). seq 1 20 >file.txt (head -5; tail -5) <file.txt
- ralph 14y agoI'm complaining his example doesn't work in the more general case and that he should warn of this or give a better more general solution. I'm aware of why it doesn't work.
- js2 14y agoFor those who don't follow why this is. In the first case, data is dup'ed to stdin and so remains seekable. This is important since head buffers reads (typically 8k at a time) for efficiency, then uses lseek to reposition to just after the requested number of lines. If reading from a pipe as in the second case, lseek() fails and by the time tail runs, head has consumed all of the file. If you use "seq 10000 | (head -5; tail -5)" you'll get the first and last lines as expected since head hasn't consumed too much of the file. I don't think this invalidates his example, but it could mention this subtle caveat. :-)