Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

For example if my file has 15 lines I want to select lines from 2 to 14.

like image 735
femchi Avatar asked Oct 03 '12 08:10

femchi


People also ask

How do I see the last second line of a file?

To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file. Try using tail to look at the last five lines of your . profile or .

How do you find the last second line of a file in Unix?

head -2 creates a file of two lines. tail -1 prints out the last line in the file.

How do I display the last line of a file?

Use the tail command to write the file specified by the File parameter to standard output beginning at a specified point. This displays the last 10 lines of the accounts file. The tail command continues to display lines as they are added to the accounts file.

How do you go to the last line of a file in Linux?

In short press the Esc key and then press Shift + G to move cursor to end of file in vi or vim text editor under Linux and Unix-like systems.


2 Answers

tail -n +2 /path/to/file | head -n -1
like image 58
itsbruce Avatar answered Sep 19 '22 19:09

itsbruce


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 '{a[count++]=$0}END{for(i=1;i<count-1;i++) print a[i]}' your_file
like image 25
Vijay Avatar answered Sep 18 '22 19:09

Vijay