diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2010-07-16 12:41:09 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2010-07-16 12:41:09 +0300 |
commit | 8c042f99cc7465c86351d21331a129111b75345d (patch) | |
tree | 9656e653be0e42e5469cec77635c20356de152c2 /awklib/eg/prog/egrep.awk | |
parent | 8ceb5f934787eb7be5fb452fb39179df66119954 (diff) | |
download | egawk-8c042f99cc7465c86351d21331a129111b75345d.tar.gz egawk-8c042f99cc7465c86351d21331a129111b75345d.tar.bz2 egawk-8c042f99cc7465c86351d21331a129111b75345d.zip |
Move to gawk-3.0.0.
Diffstat (limited to 'awklib/eg/prog/egrep.awk')
-rw-r--r-- | awklib/eg/prog/egrep.awk | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/awklib/eg/prog/egrep.awk b/awklib/eg/prog/egrep.awk new file mode 100644 index 00000000..5a5ec988 --- /dev/null +++ b/awklib/eg/prog/egrep.awk @@ -0,0 +1,96 @@ +# egrep.awk --- simulate egrep in awk +# Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain +# May 1993 + +# Options: +# -c count of lines +# -s silent - use exit value +# -v invert test, success if no match +# -i ignore case +# -l print filenames only +# -e argument is pattern + +BEGIN { + while ((c = getopt(ARGC, ARGV, "ce:svil")) != -1) { + if (c == "c") + count_only++ + else if (c == "s") + no_print++ + else if (c == "v") + invert++ + else if (c == "i") + IGNORECASE = 1 + else if (c == "l") + filenames_only++ + else if (c == "e") + pattern = Optarg + else + usage() + } + if (pattern == "") + pattern = ARGV[Optind++] + + 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) +{ + fcount = 0 +} +function endfile(file) +{ + if (! no_print && count_only) + if (do_filenames) + print file ":" fcount + else + print fcount + + total += fcount +} +{ + matches = ($0 ~ pattern) + if (invert) + matches = ! matches + + fcount += matches # 1 or 0 + + if (! matches) + next + + if (no_print && ! count_only) + nextfile + + if (filenames_only && ! count_only) { + print FILENAME + nextfile + } + + if (do_filenames && ! count_only) + print FILENAME ":" $0 + else if (! count_only) + print +} +END \ +{ + if (total == 0) + exit 1 + exit 0 +} +function usage( e) +{ + e = "Usage: egrep [-csvil] [-e pat] [files ...]" + print e > "/dev/stderr" + exit 1 +} |