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.
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.
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 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:
Using sed (stream editor): sed -n '$p' fileName
Using tail: tail -1 fileName
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With