select the second line to last line of a file select the second line to last line of a file unix unix

select the second line to last line of a file


tail -n +2 /path/to/file | head -n -1


perl -ne 'print if($.!=1 and !(eof))' your_file

tested below:

> cat temp1234567> perl -ne 'print if($.!=1 and !(eof))' temp23456> 

alternatively in awk you can use below:

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 '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!

Test

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