find files and search for within tail

5,267

Solution 1

This seems to work for me, not sure if it's the best way:

find . -name '*.log' -exec tail {} \; | grep GET | more

The main thing is executing the commands in a more correct order.

Good luck!

Solution 2

Your question is probably already answered here: https://stackoverflow.com/questions/307015/how-do-i-include-a-pipe-in-my-linux-find-exec-command

You can use perl for both tail's and grep's job, though (note: this is semi-tongue-in-cheek because of the surprising complexity, but it should work):

find /opt/ -name "somefile.out"  -exec  perl -ne 'push @a, $_; @a = @a[@a-NUMBEROFLINES..$#a]; END { foreach (@a) { print if /PATTERN/ } }' '{}' \;

If instead of a definite range of lines you want to scan after a certain "footer" is seen perl does make it easier on you:

perl -ne 'print if /footer/ .. eof() and /PATTERN/'
Share:
5,267

Related videos on Youtube

shantanuo
Author by

shantanuo

open source contributor

Updated on September 18, 2022

Comments

  • shantanuo
    shantanuo almost 2 years

    The following is working as expected.

    find /opt/ -name "somefile.out" -exec grep 'Connection refused' {} \; | more
    

    But if I want to search only in the tail of the found files, I can not add tail or tail -100 like this...

    find /opt/ -name "somefile.out" -exec grep 'Connection refused' tail {} \; | more
    

    What is the best way to search for text in the last few lines?

  • baraboom
    baraboom almost 13 years
    Nice tips, I guess perl is good for something after all!