1

I'm trying to grep some text from my application log files and I really don't understand why it's not working... First I am extracting every part of the file that might have what I want, and later I want to exclude the ones that have some words that I don't want. The thing is that I want to remove the whole block that the first grep found, not only the line. I am trying this:

grep -A 5 "header of start parts" file.log | grep -v "piece of unwanted words" -A 2 -B 3

So I thought that using the -A 2 and -B 3 would remove the whole block in the second grep, but it is not what its happening. It's still giving me results with the unwanted words... This is the pattern of my file:

Some text to ignore
Some Text to ignore
header of start parts
line to get together
line to get together
line to get together
line to get together
line to get together
Some text to ignore
header of start parts
line to get together
line with piece of unwanted words
line to get together
line to get together
line to get together
A lot of other logs and this patterns repeating

So my first grep give me all the occurrences of this:

header of start parts
line to get together
line to get together
line to get together
line to get together
line to get together

and this:

header of start parts
line to get together
line with piece of unwanted words
line to get together
line to get together
line to get together

And the second grep I want to exclude all of this second block I posted. Thanks for any help in advance.

3

Since grep helpfully delimits the context blocks (by default, using -- strings), you could pass the result of the first grep to awk in paragraph mode and exclude whole records that match the second fragment:

$ grep -A5 "header of start parts" file.log | 
  awk -vRS='\n--\n' '!/line with piece of unwanted words/'
header of start parts
line to get together
line to get together
line to get together
line to get together
line to get together

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.