: ########################################################################## # Shellscript: ngrep - news grep (ignores headers) # Author : Heiner Steven # Date : 1995-06-02 # Category : News # SCCS-Id. : @(#) ngrep 1.2 04/02/18 ########################################################################## # Description # o Search the news files for a pattern. Only print one match # per line # o Ignore headers, but match "Subject:" # # Changes # 12.12.96 hs Let nawk handle multiple files (speed improvement: 7x) (0.2) # 18.12.96 hs Only match "Subject:" header line (0.3) ########################################################################## PN=`basename "$0"` # Program name VER='%I' # Search PATH for a new AWK (or GNU AWK) for path in `echo "$PATH" | sed 's/^:/.:/;s/:$/:./;s/:/ /g'` do [ -x "$path/gawk" ] && : ${NAWK=$path/gawk} [ -x "$path/nawk" ] && : ${NAWK=$path/nawk} done : ${NAWK=awk} Usage () { echo >&2 "$PN - grep news, ignore headers, $VER (stv '95) usage: $PN search_pattern file [file...]" exit 1 } Msg () { for i do echo "$PN: $i" >&2 done } Fatal () { Msg "$@"; exit 1; } while [ $# -gt 0 ] do case "$1" in # Your flags here --) shift; break;; -h) Usage;; -*) Usage;; *) break;; # First file name esac shift done [ $# -lt 1 ] && Usage Pattern="$1"; shift $NAWK ' { # Skip rest of file if ( NextFile ) { while ( getline && FNR != 1 ) ; # Skip line NextFile = 0 } } FNR==1 { # News header do { # Scan until first blank line if ( $0 ~ /^Subject: .*('"$Pattern"')/ ) { print FILENAME " " $0 NextFile = 1 break } } while ( getline && $0 != "" ) } $0 ~ /'"$Pattern"'/ && NextFile == 0 { # Search news article body print FILENAME " " $0 NextFile = 1 } ' "$@" exit 0