简体   繁体   English

选择第二行到文件的最后一行

[英]select the second line to last line of a file

How can I select the lines from the second line to the line before the last line of a file by using head and tail in unix? 如何在unix中使用headtail选择从第二行到文件最后一行之前的行的行?

For example if my file has 15 lines I want to select lines from 2 to 14. 例如,如果我的文件有15行,我想选择2到14行。

tail -n +2 /path/to/file | head -n -1
perl -ne 'print if($.!=1 and !(eof))' your_file

tested below: 测试如下:

> cat temp
1
2
3
4
5
6
7
> perl -ne 'print if($.!=1 and !(eof))' temp
2
3
4
5
6
> 

alternatively in awk you can use below: 或者在awk中你可以使用以下:

awk '{a[count++]=$0}END{for(i=1;i<count-1;i++) print a[i]}' your_file

To print all lines but first and last ones you can use this awk as well: 要打印除了第一行和最后一行之外的所有行,您也可以使用此awk

awk 'NR==1 {next} {if (f) print f; f=$0}'

This always prints the previous line. 这始终打印上一行。 To prevent the first one from being printed, we skip the line when NR is 1. Then, the last one won't be printed because when reading it we are printing the penultimate! 为了防止第一个被打印,我们在NR为1时跳过该行。然后,最后一个将不会被打印,因为在阅读时我们正在打印倒数第二个!

Test 测试

$ seq 10 | awk 'NR==1 {next} {if (f) print f; f=$0}'
2
3
4
5
6
7
8
9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM