Stop grep after one result on Solaris

grepsolaris

I need to return the most recent value from a log file. I know this value will always be near the end of the log file, and I only ever want one result.

On Ubuntu I've accomplished this with tac dhcp.log | grep macaddress -m 1, and on Solaris I've almost replicated this with tail -r dhcp.log | grep macaddress, but it runs through the entire file which is quite large and takes too long. Is there any way to kill grep after the first result so that it doesn't run through the entire file?

Best Answer

If you have GNUgrep installed (eg /usr/bin/ggrep or /opt/gnu/bin/grep on Solaris 11, /opt/sfw/bin/ggrep on Solaris 10) then you have the -m flag.

Instead of grep you could use sed

sed -n '/macaddress/{ p
q
}'
Related Question