Re: Reading Log



2008-06-10, 18:17(+02), Marcel Bruinsma:
Anyway, I was thinking, is there a way to do a "grep" that starts at
the end of file and STOPS on the first match ? So they could add as
many lines they wanted.

tac log.log | sed '/xyz/!d;q' | cut ...

However, tac doesn't really start at the end of the file,
it reads lines from beginning to end, like tail. It just
prints output from end to begin.

You might as well skip tac:

sed '/xyz/!{$!d;g;q};$q;h;d' log.log | cut ...
[...]

ITYM: sed '/xyz/!{H;$!d;g;};$q;h;d'
or: sed '/xyz/{$q;h;d;};H;$!d;g'

which assumes the file does contain the pattern

Note that many sed implementations have a limited hold or
pattern space size (IIRC, POSIX requires it to hold at least 10
lines), so the above will not necessarily be very reliable
(there's a missing ";" after the first q BTW).

Also tac (see also tail -r on some systems) does read from the
end (by doing lseeks), so for big files, it will be more
efficient.

--
Stéphane
.



Relevant Pages

  • Re: Reading Log
    ... the end of file and STOPS on the first match? ... However, tac doesn't really start at the end of the file, ... prints output from end to begin. ...
    (comp.unix.shell)
  • Re: Reading Log
    ... Marcel Bruinsma wrote: ... the end of file and STOPS on the first match? ... However, tac doesn't really start at the end of the file, ...
    (comp.unix.shell)
  • Re: Reading Log
    ... the end of file and STOPS on the first match? ... However, tac doesn't really start at the end of the file, ... prints output from end to begin. ...
    (comp.unix.shell)
  • Re: How to find the last line that contains a word in file
    ... If you have 'tail -r' or 'tac' it is probably most efficient to ...
    (comp.unix.shell)
  • Re: finding the last item
    ... - indeed I suspect the tac based approach would actually be slower. ... But note that tac is a GNU command and not a Unix command. ... is the -m grep option. ... I agree that grep | tail -n 1 is probably the ...
    (comp.unix.shell)