bash: lines of a file in reverse order?

Gregory D Hough mr6re9-mI4xJ4qlgtBiLUuM0BA3LQ at public.gmane.org
Fri Aug 25 03:22:56 UTC 2006


>    Use tac:
> 
> tac filename
> 
>> I want to find the "last matching line" in a large log file.  My hope
>> was:
>>
>> tail -50 -r filename | grep -m 1 "matching"
>> would grab it for me if it exists in the last 50 lines of the log.
> 
> 
> grep "PATTERN" filename | tail -n1
> 
A
#!/bin/bash
grep "INVALID" /var/log/messages | tail -n1

  time sh A
Aug 24 15:52:22 localhost kernel: INVALID: IN=eth1 OUT= SRC=69.63.65.165

real    0m0.044s
user    0m0.001s
sys     0m0.015s


B
#!/bin/bash
tail -50 /var/log/messages | tac | grep -m1 "INVALID"

  time sh B
Aug 24 15:52:22 localhost kernel: INVALID: IN=eth1 OUT= SRC=69.63.65.165

real    0m0.048s
user    0m0.004s
sys     0m0.016s

Yep, method A is 4 to 5 thou faster than B most of the time
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list