# filename: paragrep.awk # author: Eric Pement # date: March 23, 2002, 2008-09-17, 2009-01-22, 2012-04-03 # # purpose: awk script displays a paragraph if the search expression # occurs anywhere in the paragraph. Each paragraph must be # separated from other paragraphs by a fully blank line. # # syntax: awk -v search="regex" -f c:\eric_p\bat\lib\awk_pement\paragrep.awk myfile # # where 'regex' is any valid awk regular expression. However, # interval expressions like \{4,9\} are not supported without # an additional switch, --re-interval on the command line. BEGIN { # was: RS=""; FS="\n"; ORS="\n\n"; OFS="\n"; # set paragraph mode RS="\n *\n"; FS="\n"; ORS="\n\n"; OFS="\n"; # set paragraph mode if (search == "") { print "\aNo search term! Exiting..."; exit 1 } if (icase == "t") IGNORECASE = 1 # 'verbose' is another variable that may exist. } $0 ~ search { print; a++ } END { if (search == "") exit 1 i_matches = a+0 # coerce a into an integer time_plur = i_matches == 1 ? "time" : "times" if ( verbose ) { printf ("Search matched %i %s.\n", i_matches, time_plur ) printf ("The expression to search for was [%s].\n", search) } else { if (i_matches > 0) printf ("Matched %i %s on expression %s.\n", i_matches, time_plur, search) } } #---end of script---