0

I'm using sed to return the lines between two different patterns [(1) <Directory>, (2) </Directory>]. For the given input file, there are multiple matches for sed, and I was wondering if it is possible to pipe each individual match.

What I'm trying to do basically: sed -n "/ddd/,/</ddd>p" < input.conf | grep Options

...
ddd    Start of Match group #1
eee
fff
/ddd   End of Match group #1
...
ddd    Start of Match group #2
iii
yyy
/ddd   End of Match group #2

I want to check to see if each match group contains the pattern fff.

0

Given the complex set of requirements, awk is a better match for this problem.

An equivalent to the sed printing from start to end is (in awk):

$ awk '/^ddd/{p=1};p;/^[/]ddd/{p=0}' file
ddd    Start of Match group #1
eee
fff
/ddd   End of Match group #1
ddd    Start of Match group #2
iii
yyy
/ddd   End of Match group #2

Using the ^ ensures that the amtch is at the start of the line, and the [\] will match only a \ also at the beggining of the line.

Switching the tests, we can remove the start and end lines:

$ awk '/^[/]ddd/{p=0};p;/^ddd/{p=1}' file
eee
fff
iii
yyy

All that remains to test is if (while inside one range) there is any line with fff. That could be done with:

awk ' /^[/]ddd/{p=0;if(found==0){print NR};found=0};
      p&&($0~/fff/){found=1};
      /^ddd/{p=1}
    ' file

Which will print the line number (NR) of the last line of the group that did not contain fff inside.

0

Try

sed -n '/ddd/ {:L; N; /\/ddd/!bL; /fff/ !{p; =;} }' file

When encountering ddd, it collects the lines up to /ddd in pattern space, then tests for fff and, if absent, prints the group and the group's last line number.

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.