I wrote a C program awhile back to summarize a text file by doing both a head
and tail
at the same time, with only a single readthrough of piped input. Example:
$ headtail -h 3 -t 3 < /tmp/x10
line01
line02
line03
... 4 output lines omitted ...
line08
line09
line10
It works, but I feel dirty by not having a nifty sed
alias that can do this. Having found this SO answer that uses sed
to print the last N lines, it seems achievable now, but I'm not quite there.
For example, the individual head
and tail
work:
$ sed -n -e '1,3p' < /tmp/x10
line01
line02
line03
$ sed -n -e ':a; $p; N; 4,$D; ba' < /tmp/x10
line08
line09
line10
But my attempt at combining the two fails:
$ sed -n -e '1,3p; :a; $p; N; 4,$D; ba' < /tmp/x10
line01
line08
line09
line10
It'd also be nice for it to work if H+T > N lines in the file (act like cat
), and also for it to print a separator indicating that some lines were omitted from the middle (the number omitted would be nice, but I could live without it).