Start_pattern abc d End_pattern Start_pattern abc d ef ghij klm no End_pattern Start_pattern abc def hij End_pattern Start_pattern abc dhi jklm End_pattern
$ cat tst.awk /Start_pattern/ { fnd=1; buf="" } fnd { buf = buf $0 ORS if (/End_pattern/) { if (buf ~ /ef/) { printf "%s", buf } fnd = 0 buf = "" } } $ awk -f tst.awk file Start_pattern abc d ef ghij klm no End_pattern Start_pattern abc def hij End_pattern