我怎样才能通过使用unix中的head
和tail
从第二行到文件的最后一行之前的行select行?
例如,如果我的文件有15行,我想select2到14行。
tail -n +2 /path/to/file | head -n -1
perl -ne 'print if($.!=1 and !(eof))' your_file
测试如下:
> cat temp 1 2 3 4 5 6 7 > perl -ne 'print if($.!=1 and !(eof))' temp 2 3 4 5 6 >
或者在awk中你可以使用如下:
awk '{a[count++]=$0}END{for(i=1;i<count-1;i++) print a[i]}' your_file
要打印所有的行,除了第一个和最后一个,你也可以使用这个awk
:
awk 'NR==1 {next} {if (f) print f; f=$0}'
这总是打印上一行。 为了防止第一个被打印,我们在NR
为1时跳过这一行。然后,最后一个将不会被打印,因为在阅读时我们正在打印倒数第二个!
$ seq 10 | awk 'NR==1 {next} {if (f) print f; f=$0}' 2 3 4 5 6 7 8 9