Heads and Tails - Trying to get first line and last ten lines of each file

2k views Asked by At

I've got a directory of output files that I'd like to display the first line of each file and the last ten lines of each file in order.

I've got part of the command down:

ls output/*Response | sort -t_ --key=2 -g | xargs tail | less

Which give me something like this:

==> output/Acdb_18_Response <==
150707,"SOVO","Other","","","","","","160x600",0,0,1432,0,0,1432
167493,"Asper","Other","","","","","","160x600",143200,0,0,1432,0,0
269774,"AIKA","Other","","","","","","160x600",0,1432,0,0,1432,0
342275,"Lorrum","Other","","","","","","160x600",0,0,1432,0,0,1432
347954,"Game","Other","","","","","","160x600",0,1432,0,0,1432,0
418858,"Technologies","Other","","","","","","160x600",0,1432,0,0,1432,0
24576,"Media ","Other","","","","","","300x600",0,0,1432,0,0,1432
23351," Plus","Other","","","","","","425x600",0,4296,0,0,4296,0
#rowcount=79

which is nice but I'd like to include the first line to get the header. I tried tee'ing the output to head but so far I haven't been able to figure out how to arrange the pipes.

Any suggestions?

2

There are 2 answers

0
Alexander Pogrebnyak On BEST ANSWER
ls output/*Response | sort -t_ --key=2 -g \
    | xargs -I {} sh -c 'head -1 {}; tail {}' | less
0
hjpotter92 On

You can also try the following:

ls output/*Response | sort -t_ --key=2 -g | ((head -n 1) && (tail -n 10)) | less