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

Issue

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.

Solution

Try this:

tail -2 yourfile | head -1

Answered By – MattH

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published