Selecting specific variable in log file

Hi there

I am trying to look for a specific word in the log file and I am aware this can be done by grep for example.

As there will be multiple entries for this I want to grep the last one to enter the log... how would I go about this - would I have to use tail?

Thanks in advance

Alex

Did you try:

grep pattern filename | tail -1

How about

tac file | grep -m1 pattern

? It will save reading / searching through the entire file, esp. interesting for large files.

tac does read the entire file.?
With sed

sed -n '/pattern/x;$x;$p' file

No, it doesn't. When grep stops reading the pipe because it found the pattern and exited, tac fills it until it's full and then is killed with SIGPIPE:

lseek(3, 311877632, SEEK_SET)           = 311877632
read(3, "rag\nTestbeitrag\nTestbeitrag\nTest"..., 8192) = 8192
write(1, "itrag\nTestbeitrag\nTestbeitrag\nTe"..., 4096) = -1 EPIPE (Broken pipe)
--- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=4061, si_uid=1000} ---
+++ killed by SIGPIPE +++

As you can see, 311869440 (311877632 - 8192) bytes from the file have NOT been read.

1 Like