Operating System - Linux
1752812 Members
6258 Online
108789 Solutions
New Discussion юеВ

Re: awk to print last line

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

awk to print last line

Hi,

how can I get awk to print the last line and first field from a command output?

I know I can do :

command | awk '/rows/ {print $1}'

but I was wondering is there another method incase there are mulitple lines containing rows?

command output is:

USER_NAME RESPONSIBILITY
--------------- ------------------------------
abc

USER_NAME RESPONSIBILITY
--------------- ------------------------------
def

141 rows selected.
hello
8 REPLIES 8
Pete Randall
Outstanding Contributor
Solution

Re: awk to print last line

Not sure about awk but I know you can do it with sed:

# print last line of file (emulates "tail -1")
sed '$!d'


Pete

Pete
lawrenzo_1
Super Advisor

Re: awk to print last line

super,

that will do me!

Thanks Pete.
hello
Ivan Krastev
Honored Contributor

Re: awk to print last line

# print the last line of a file (emulates "tail -1")
awk 'END{print}'


regards,
ivan
James R. Ferguson
Acclaimed Contributor

Re: awk to print last line

Hi Chris:

With Perl that's easy:

# command|perl -nlae 'print $F[0] if eof'

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: awk to print last line

Hi (again):

...I should hasten to add, that if you want the whole last line, simply do:

# command|perl -nle 'print if eof'

...otherwise what I gave above prints the _first_ filed of the _last_ line only.

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: awk to print last line

You could of course use tail then awk. Or use this script that saves field 1 of the last line:
command | awk '
{
SAVE = $1
}
END { print SAVE } '
lawrenzo_1
Super Advisor

Re: awk to print last line

Thats the ticket ....

Thank you everyone.

Chris.
hello
Dennis Handly
Acclaimed Contributor

Re: awk to print last line

It looks like you can combine Ivan's with mine and get:
awk 'END { print $1 } '