2

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?

shantanuo
  • 3,579
  • 8
  • 49
  • 66

2 Answers2

3

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!

baraboom
  • 236
  • 2
  • 4
1

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/'
Eduardo Ivanec
  • 14,881
  • 1
  • 37
  • 43