Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

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
add comment

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