tags:

views:

94

answers:

4

How to print just the last line of a file?

+2  A: 

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

RonK
because tail not fast as awk
yael
@yael Not at all; `tail -1` will be way faster than `awk` for a large file. I tried on an ~3m line file; tail was instantaneous while awk took .44s
Michael Mrozek
The `-1` form is deprecated. Use `tail -n 1`.
Dennis Williamson
@yael. tail is specifically meant for processing the file from the "end". Therefore, its faster than awk. Because awk processes files from the beginning. The algorithms are different.
ghostdog74
A: 

if you must use awk then try

$ awk '
{X=$0}
 END{print $0}' junk.lst

substitute your filename for junk.lst

josephj1989
You probably meant `END {print X}` but it's not necessary to capture every line.
glenn jackman
+2  A: 
END{print}

should do it. Thanks to Ventero who was too lazy to submit this answer.

Joey
Oh, that's much simpler than my way. Nice
Michael Mrozek
+1  A: 

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 -1 file

If you really want to use awk,

awk 'END{print}' file
ghostdog74