I have a text file that contains text blocks like this:
IN
hit
ER 123 hit 456
abc
hit
ghi
ER 789 hit 012
abc
ghi
IN 345
abc
def
ghi
ER 678 xxx 901
xyz
hit
xyz
IN
risk
in
Blocks can have any number of lines, but always start with line containing ER or IN.
Using awk how can I select lines which are occurring between two similar marker patterns?
1) There may be multiple sections marked with these patterns.
2) One of selected lines between patterns must contain another pattern (eg. hit)
3) Line with first pattern (eg. ER) should be included, line with second one (eg. ER|IN) should be excluded.
Expected output:
ER 123 hit 456
abc
hit
ghi
ER 678 xxx 901
xyz
hit
xyz
I've tried to achieve my goal with
awk '/ER/ {block=1} block {str=str sep $0; sep=RS} /ER|IN/ {block=0; if (str~/hit/) {print str} str=sep=""}'
but it gives me
ER abc hit ghi
ER 789 hit 012
EDIT: my example wasn't precise enough. EDIT2:
a) I try to find line matching pattern " ER " b) I search for nearest next line matching pattern " ER " or " IN " c) I want to print result only if my result contains at least one line matching pattern ".hit.", but it can't be first line. Result should include first line, but exclude last line, so:
ER 678 xxx 901
xyz
hit
xyz
should be printed, because there is one line matching hit in block between line matching " ER " and line matching " IN "
ER 789 hit 012
abc
ghi
shouldn't be printed, because there is no line matching hit in block between line matching " ER " and line matching " IN "