phileas fogg
phileas fogg

Reputation: 1933

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

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?

Upvotes: 1

Views: 2026

Answers (2)

hjpotter92
hjpotter92

Reputation: 80639

You can also try the following:

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

Upvotes: 0

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

ls output/*Response | sort -t_ --key=2 -g \
    | xargs -I {} sh -c 'head -1 {}; tail {}' | less

Upvotes: 2

Related Questions