i have a log file from postgresql that has log entries of the format;
LOG: execute <unnamed>: /match this here/
DETAIL: parameters: /also want to filter this line/
I thought it might be possible with
grep -v --after-context=1 'match this here' /my/log/file.log
but it doesn't work. is there some sed/awk/grep magic, or do i have to do some perl?
As far as I know you cannot do this with grep alone.
awk can do it pretty simple:
awk -v skip=-1 '/match this here/ { skip = 1 } skip-- >= 0 {next } 1' /my/log/file.log
edit:
I assumed that you cannot match the second line by any regex and you really want to match line containing X and the line afterwards. If you can match against a string, it's a lot easier:
egrep -v "match this here|also want to filter this line" /my/log/file.log
Another way to use awk:
Are you intending to use "-v" to prevent 'match this here' from displaying the first line? "-v" inverts your expression to say "display lines that don't match 'match this here'". I'm not on an *nix box right now but I think you could do something like this if you only wanted the second line to show:
This should print both lines but the second grep statement should hide the first line that starts with LOG:
Try that and good luck.
It's not entirely clear what you want to achieve. If you want to /match this here/ and then print the next line then
If you want to /match this here/ and then print the matched line and the one following it then
With grep you can
if you have multiple matches each is separated by
--
And you can
to remove the first line if that's what you want.
LOG: execute : /match this here/ DETAIL: parameters: /also want to filter this line/
STR='match this here' FILE=/var/log/logfile.log
sed "/$STR/{n;d;}" $FILE | sed /$STR/d