Say you have a txt file, what is the command to view the top 10 lines and bottom 10 lines of file simultaneously?
i.e. if the file is 200 lines long, then view lines
To handle pipes (streams) as well as files, add this to your .bashrc or .profile file:
headtail() { awk -v offset="$1" '{ if (NR <= offset) print; else { a[NR] = $0; delete a[NR-offset] } } END { for (i=NR-offset+1; i<=NR; i++) print a[i] }' ; }
Then you can not only
headtail 10 < file.txt
but also
a.out | headtail 10
(This still appends spurious blank lines when 10 exceeds the input's length, unlike plain old a.out | (head; tail)
. Thank you, previous answerers.)
Note: headtail 10
, not headtail -10
.