Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Say I want to grab the last 5 mentions of cron in /var/log/syslog.log looking over the grep manual it seems like I can't get grep to search in reverse. I can however grab the most recent occurrences.

grep cron -m 5 /var/log/syslog

Is there a reverse grep command I can use? :)

share|improve this question

2 Answers 2

up vote 10 down vote accepted

Following the usual unix philosophy of combining tools: use grep to search, and tail to return the last part of the file.

grep cron /var/log/syslog | tail -n 5
share|improve this answer

What about piping to tac?

http://linux.about.com/library/cmd/blcmdl1_tac.htm

tac  /var/log/syslog | grep cron -m 5
share|improve this answer
    
You need a second tac in the end, to reconstruct the original order, no tac /var/log/syslog | grep cron -m 5 | tac? Tacatac ... :) –  user unknown May 7 '11 at 20:02
    
But isn't reverse order part of the request? Taaaac ^^ –  neurino May 7 '11 at 22:22
    
Ah, maybe! I understood the reverse-part only as searching for the last ones. –  user unknown May 8 '11 at 0:31

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.