I have a file with about 30.000.000 lines (Radius Accounting) and I need to find the last match of a given pattern.
The command:
tac accounting.log | grep $pattern
gives what I need, but it's too slow because the OS has to first read the whole file and then send to the pipe.
So, I need something fast that can read the file from the last line to the first.
Best Answer
tac
only helps if you also usegrep -m 1
(assuming GNUgrep
) to havegrep
stop after the first match:From
man grep
:In the example in your question, both
tac
andgrep
need to process the entire file so usingtac
is kind of pointless.So, unless you use
grep -m
, don't usetac
at all, just parse the output ofgrep
to get the last match:Another approach would be to use Perl or any other scripting language. For example (where
$pattern=foo
):or