gawk '/BEGIN\(EXTRACT\)/ { if (depth++) { print "nested extracts not allowed on line: " NR depth = 1 } print "" next } /END\(EXTRACT\)/ { if (--depth) { print "too many END(EXTRACT) s on line " NR depth = 0 } next } depth > 0' "$*"