Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Show only text between 2 matching pattern

In a bash script using sed how can I remove a block of lines beginning with -pattern a- and ending with -pattern b- where the the contents contains -pattern c- ( or does NOT contain -pattern c-)?

So :

line 1 -pattern a-  
line 2   
line 3 -pattern b-  
line 4 -pattern a-   
line 5 -pattern c-  
line 6 -pattern b-

In this example I want to remove lines 4,5 and 6 (or remove 1,2 and 3 for not containing -pattern c-).

share|improve this question

marked as duplicate by jasonwryan, Renan, rahmu, Gilles, Mat Nov 8 '12 at 6:48

This question was marked as an exact duplicate of an existing question.

Assuming you will always have three consecutive lines, you can use the following:

# Print with pattern c
awk 'NR % 3 !=0 {printf $0;printf "|"} NR % 3 ==0 {printf $0; print " "}' file.txt | grep "\-pattern\ c\-" | sed 's/|/\n/g'

# Print without pattern c
awk 'NR % 3 !=0 {printf $0;printf "|"} NR % 3 ==0 {printf $0; print " "}' file.txt | grep -v "\-pattern\ c\-" | sed 's/|/\n/g'
share|improve this answer
Thanks for your answer The problem is that it is a block with variable quantity of lines and the -pattern c- is not always at the same line – k.mooijman Nov 8 '12 at 6:15

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