Skip to content

Instantly share code, notes, and snippets.

@vtta
Created January 6, 2023 07:26
Show Gist options
  • Save vtta/031fc8d079d81a212ae429eb7a8bab88 to your computer and use it in GitHub Desktop.
Save vtta/031fc8d079d81a212ae429eb7a8bab88 to your computer and use it in GitHub Desktop.
#!/usr/bin/awk -f
# https://unix.stackexchange.com/a/495105
# usage: match_block START END MATCH [FILE]
# output a whole block of text enclosed by START and END that contains MATCH
BEGIN {
pstart=ARGV[1];
pstop=ARGV[2];
pmatch=ARGV[3];
ARGV[1]=ARGV[4];
ARGC=2;
}
# exp ~ /regexp/ means exp matches the regex
$0 ~ pstart { block = $0; output = 0; next }
{ block = block "\n" $0 }
$0 ~ pmatch { output = 1 }
$0 ~ pstop && output { print block; output = 0 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment