头部和尾部 - 尝试获取每个文件的第一行和最后十行

时间:2011-10-28 23:01:15

标签: shell unix pipe head tail

我有一个输出文件目录,我想按顺序显示每个文件的第一行和最后十行。

我已经完成了命令的一部分:

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

给我这样的东西:

==> 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

这很好,但我想包括第一行来获取标题。我尝试将输出发送到头部,但到目前为止我还没弄清楚如何安排管道。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

您还可以尝试以下操作:

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