diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2020-09-13 21:02:08 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2020-09-13 21:02:08 +0300 |
commit | 696533f3f4a3b2dd36042eac5e1dce6f9bcec129 (patch) | |
tree | 66b2383f63ce6ed998bf4e05bc88413ede1138bd /awklib/eg/prog/egrep.awk | |
parent | 7d08b2cd4dd4af16bac395bf1b93d7ed03cdca09 (diff) | |
download | egawk-696533f3f4a3b2dd36042eac5e1dce6f9bcec129.tar.gz egawk-696533f3f4a3b2dd36042eac5e1dce6f9bcec129.tar.bz2 egawk-696533f3f4a3b2dd36042eac5e1dce6f9bcec129.zip |
Update egrep program to be POSIX compliant.
Diffstat (limited to 'awklib/eg/prog/egrep.awk')
-rw-r--r-- | awklib/eg/prog/egrep.awk | 67 |
1 files changed, 41 insertions, 26 deletions
diff --git a/awklib/eg/prog/egrep.awk b/awklib/eg/prog/egrep.awk index a4165a90..8ec5105b 100644 --- a/awklib/eg/prog/egrep.awk +++ b/awklib/eg/prog/egrep.awk @@ -2,58 +2,67 @@ # # Arnold Robbins, arnold@skeeve.com, Public Domain # May 1993 +# Revised September 2020 # Options: # -c count of lines -# -s silent - use exit value -# -v invert test, success if no match +# -e argument is pattern # -i ignore case # -l print filenames only -# -e argument is pattern +# -n add line number to output +# -q quiet - use exit value +# -s silent - don't print errors +# -v invert test, success if no match +# -x the entire line must match # -# Requires getopt and file transition library functions +# Requires getopt library function +# Uses IGNORECASE, BEGINFILE and ENDFILE +# Invoke using gawk -f egrep.awk -- options ... BEGIN { - while ((c = getopt(ARGC, ARGV, "ce:svil")) != -1) { + while ((c = getopt(ARGC, ARGV, "ce:ilnqsvx")) != -1) { if (c == "c") count_only++ - else if (c == "s") - no_print++ - else if (c == "v") - invert++ + else if (c == "e") + pattern = Optarg else if (c == "i") IGNORECASE = 1 else if (c == "l") filenames_only++ - else if (c == "e") - pattern = Optarg + else if (c == "n") + line_numbers++ + else if (c == "q") + no_print++ + else if (c == "s") + no_errors++ + else if (c == "v") + invert++ + else if (c == "x") + full_line++ else usage() } if (pattern == "") pattern = ARGV[Optind++] + if (pattern == "") + usage() + for (i = 1; i < Optind; i++) ARGV[i] = "" + if (Optind >= ARGC) { ARGV[1] = "-" ARGC = 2 } else if (ARGC - Optind > 1) do_filenames++ - -# if (IGNORECASE) -# pattern = tolower(pattern) } -#{ -# if (IGNORECASE) -# $0 = tolower($0) -#} -function beginfile(junk) -{ +BEGINFILE { fcount = 0 + if (ERRNO && no_errors) + nextfile } -function endfile(file) -{ +ENDFILE { if (! no_print && count_only) { if (do_filenames) print file ":" fcount @@ -64,7 +73,10 @@ function endfile(file) total += fcount } { - matches = ($0 ~ pattern) + matches = match($0, pattern) + if (matches && full_line && (RSTART != 1 || RLENGTH != length())) + matches = 0 + if (invert) matches = ! matches @@ -83,7 +95,10 @@ function endfile(file) } if (do_filenames) - print FILENAME ":" $0 + if (line_numbers) + print FILENAME ":" FNR ":" $0 + else + print FILENAME ":" $0 else print } @@ -93,7 +108,7 @@ END { } function usage() { - print("Usage: egrep [-csvil] [-e pat] [files ...]") > "/dev/stderr" - print("\n\tegrep [-csvil] pat [files ...]") > "/dev/stderr" + print("Usage:\tegrep [-cilnqsvx] [-e pat] [files ...]") > "/dev/stderr" + print("\tegrep [-cilnqsvx] pat [files ...]") > "/dev/stderr" exit 1 } |