Skip to content

Instantly share code, notes, and snippets.

@jarfil
Last active February 20, 2022 23:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarfil/2f24d15c7ba534500eef to your computer and use it in GitHub Desktop.
Save jarfil/2f24d15c7ba534500eef to your computer and use it in GitHub Desktop.
Find blocks of text, separated by newline, with occurring regexp
#!/usr/bin/awk -E
BEGIN {
if ( ARGV[1] == "" ) {
print "Usage: grep-block <regexp> [file1 file2 ...]"
exit
}
search = ARGV[1]
print "-" search "-"
ARGV[1]=""
IGNORECASE = 1;
found = 0;
}
$0 ~ search {
found = 1
}
// { # always
out = out $0 "\n"
}
/$^/ { # empty line (block end)
if ( found == 1 ) {
print out
}
out = ""
found = 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment