0

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-).

1 Answers1

0

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'
user26138
  • 29
  • 1
  • 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 08 '12 at 06:15