Operating System - HP-UX
1828404 Members
3471 Online
109977 Solutions
New Discussion

Re: How to display last line with awk?

 
SOLVED
Go to solution
ericfjchen
Regular Advisor

How to display last line with awk?

awk 'NR==4{print $1}' /tmp/test.log
---
The above can display forth line of the file /tmp/test.log. How to display the last line... NR==?

Thanks

Eric
17 REPLIES 17
Leif Halvarsson_2
Honored Contributor

Re: How to display last line with awk?

hi,

try

'END {print $1}'
Kasper Hedensted
Trusted Contributor

Re: How to display last line with awk?

Do you need to do this with awk ?

If not then use tail

tail -1 /tmp/test.log

Cheers,
Kasper
Joseph Loo
Honored Contributor

Re: How to display last line with awk?

hi,

not sure how to use awk to display last line, but using tail:

# tail -n 1 /tmp/test.log

regards.
what you do not see does not mean you should not believe
Cem Tugrul
Esteemed Contributor

Re: How to display last line with awk?

Eric,

tail -1 /tmp/test.log

good luck,
Our greatest duty in this life is to help others. And please, if you can't
Rick Garland
Honored Contributor

Re: How to display last line with awk?

Use the tail command as mentioned.

tail -1

Rick Garland
Honored Contributor

Re: How to display last line with awk?

Use the tail command as mentioned.

tail -1

This is 1 line to get what you want. Doing so with awk will be more lines and more complexity
Kent Ostby
Honored Contributor

Re: How to display last line with awk?

Eric --

awk '{saveit=$0;}END{print saveit}' /tmp/test.log
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Sandman!
Honored Contributor

Re: How to display last line with awk?

Eric...tail is your best choice though you can use sed as well:

# tail -1

# sed -n '$p'
Marvin Strong
Honored Contributor

Re: How to display last line with awk?

awk 'END{print $0}' file

will give you the entire last night of the file.
Torsten.
Acclaimed Contributor

Re: How to display last line with awk?

Hi Eric,

it depends from what you want to get.

awk 'END {print $1}' ... will give you a field (only) of the last "readable" line, but
awk 'END {print $0}' ... will give you the really last line - EOT (just something like nothing). tail is a good tool to get what you want.

HTH

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Sandman!
Honored Contributor

Re: How to display last line with awk?

The only way awk will show the last line has been mentioned by Kent i.e

# awk '{lst=$0} END{print lst}'

awk 'END {print $0}' # will not show the last line of the file.

Leif Halvarsson_2
Honored Contributor

Re: How to display last line with awk?

Hi,
Yes,
awk 'END {print $0}' /tmp/test.log
don't work but,
awk 'END {print $1}' /tmp/test.log
does work (and, also $2 $3 etc.).

I used $1 as it was used in the initial example.
awk 'NR==4{print $1}' /tmp/test.log


ericfjchen
Regular Advisor

Re: How to display last line with awk?

Thanks for your help.

Yes, the following can display the last line
------------
awk '{saveit=$0;}END{print saveit}' /tmp/test.log
------------
However, I would like to get the first field of the last line..... How can I do?

Thanks again


Muthukumar_5
Honored Contributor

Re: How to display last line with awk?

Try as,

# awk 'END{ print $1 }' /tmp/test.log

hth.
Easy to suggest when don't know about the problem!
Sandman!
Honored Contributor
Solution

Re: How to display last line with awk?

# awk 'END{ print $1 }' /tmp/test.log

will print the first field of the last line as will:

# tail -1 .profile | cut -d" " -f1

cheers!
Leif Halvarsson_2
Honored Contributor

Re: How to display last line with awk?

Hi,
My previous reply do exact what you ask for, but, you can also try :

awk '{saveit=$0;}END{getline saveit ; print $1}' /tmp/test.log

There is always more then one way.
ericfjchen
Regular Advisor

Re: How to display last line with awk?

It works...
# tail -1 .profile | cut -d" " -f1