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?

link|improve this question

feedback

2 Answers

up vote 1 down vote accepted

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!

link|improve this answer
feedback

Your question is probably already answered here: http://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/'
link|improve this answer
Nice tips, I guess perl is good for something after all! – baraboom Jun 30 '11 at 21:20
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.