Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 2 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!

share|improve this answer
add comment

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/'
share|improve this answer
    
Nice tips, I guess perl is good for something after all! –  baraboom Jun 30 '11 at 21:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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