Print the last line of a file, from the CLI

LinuxShellAwkCommand Line-Interface

Linux Problem Overview


How to print just the last line of a file?

Linux Solutions


Solution 1 - Linux

$ cat file | awk 'END{print}'

Originally answered by Ventero

Solution 2 - Linux

Use the right tool for the job. Since you want to get the last line of a file, tail is the appropriate tool for the job, especially if you have a large file. Tail's file processing algorithm is more efficient in this case.

tail -n 1 file

If you really want to use awk,

awk 'END{print}' file

EDIT : tail -1 file deprecated

Solution 3 - Linux

Is it a must to use awk for this? Why not just use tail -n 1 myFile ?

Solution 4 - Linux

Find out the last line of a file:

  1. Using sed (stream editor): sed -n '$p' fileName

  2. Using tail: tail -1 fileName

  3. using awk: awk 'END { print }' fileName

Solution 5 - Linux

You can achieve this using sed as well. However, I personally recommend using tail or awk.

Anyway, if you wish to do by sed, here are two ways:

Method 1:

sed '$!d' filename

Method2:

sed -n '$p' filename

Here, filename is the name of the file that has data to be analysed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyaelView Question on Stackoverflow
Solution 1 - LinuxJoeyView Answer on Stackoverflow
Solution 2 - Linuxghostdog74View Answer on Stackoverflow
Solution 3 - LinuxRonKView Answer on Stackoverflow
Solution 4 - LinuxrishabhView Answer on Stackoverflow
Solution 5 - LinuxTapan AvasthiView Answer on Stackoverflow