Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the last line of a file, from the CLI

People also ask

How do I see the last line of a file in Linux?

To display the last part of the file, we use the tail command in the Linux system. The tail command is used to display the end of a text file or piped data in the Linux operating system. By default, it displays the last 10 lines of its input to the standard output. It is also complementary of the head command.

How do you get the last line from a file using just the terminal?

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 you print the last line of a file in Unix using SED?

In sed, $ indicates the last line, and $p tells to print(p) the last line($) only. 4. Another option in sed is to delete(d) all the lines other than(!) the last line($) which in turn prints only the last line.

What command is used to output last part of files?

What is the tail command? The tail command is a command-line utility for outputting the last part of files given to it via standard input. It writes results to standard output.


$ cat file | awk 'END{print}'

Originally answered by Ventero


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


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


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


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.