Last active
February 20, 2022 23:25
-
-
Save jarfil/2f24d15c7ba534500eef to your computer and use it in GitHub Desktop.
Find blocks of text, separated by newline, with occurring regexp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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