last line from grep resullt

Hi,

I have a grep command returns more than 20 lines. I need to display the last line of the grep result that i get.
Can someone please help me with this ?

Thanks,
Sateesh

Hello,
You can just redirect that output to sed and do something like this--

Your grep command | sed -n 20p

Thanks
Namish

How about ....

<grep command> | tail -1

last line from the list of search patterns

awk '/result/ { save=$0 }END{ print save }' filename

I needed to get the local IP of devices getting their addresses from DHCP by mac address.

Unfortunately, the file was long and devises had multiple IPs. Figuring that the last instance of the IP was the current IP I used cat, grep, tail, sed and cut in one line.

I am sure there is a better way to do this but at least it works.

cat /var/lib/dhcp/dhcpd.leases | grep 00:04:f2:12:05:90 -B5 | tail -6 | sed -n '1p' | cut -d' ' -f2

Hope this helps.

As far as I know, there's not option to grep for 'only return last line'.
As ajcannon said; piping the output into tail -1 is probably your best bet.