awk

Print lines between patterns with awk


Going back to this discussion: Print all lines between two patterns, exclusive, first instance only (in sed, AWK or Perl)

The proposed solution fails once the ending pattern is a substring of the start pattern.

Example input:

aaa
PATTERNSTART
bbb
ccc
ddd
PATT
eee

Produces output failure:

awk '/PATT/{exit} f; /PATTERNSTART/{f=1}' dat

Gives empty return instead of expected

bbb
ccc
ddd

Corner cases:

Not sure I found all corner cases. Corner cases beyond above might be treated canonically . Thanks.


Solution

  • You must use regex start and end anchors to avoid matching partial patterns:

    awk 'f && /^PATT$/{exit} f; /^PATTERNSTART$/{f=1}' dat
    
    bbb
    ccc
    ddd
    

    or else use string comparison and avoid regex altogether:

    awk 'f && $0 == "PATT"{exit} f; $0 == "PATTERNSTART"{f=1}' dat