Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the second-to-last line in a file using Bash?

Tags:

linux

bash

shell

I have a file that has the following as the last three lines. I want to retrieve the penultimate line, i.e. 100.000;8438; 06:46:12.

. . . 99.900; 8423;   06:44:41 100.000;8438;   06:46:12 Number of patterns: 8438 

I don't know the line number. How can I retrieve it using a shell script? Thanks in advance for your help.

like image 818
World Avatar asked Aug 11 '11 07:08

World


People also ask

How do we display the last second 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 I read the last line of a file in shell?

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.

How do I print last two lines in Linux?

tail [OPTION]... [ Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates.


1 Answers

Try this:

tail -2 yourfile | head -1 
like image 95
MattH Avatar answered Sep 18 '22 21:09

MattH