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/wc.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/wc.awk')
-rw-r--r-- | awklib/eg/prog/wc.awk | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/awklib/eg/prog/wc.awk b/awklib/eg/prog/wc.awk new file mode 100644 index 00000000..e9898159 --- /dev/null +++ b/awklib/eg/prog/wc.awk @@ -0,0 +1,68 @@ +# wc.awk --- count lines, words, characters +# Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain +# May 1993 + +# Options: +# -l only count lines +# -w only count words +# -c only count characters +# +# Default is to count lines, words, characters + +BEGIN { + # let getopt print a message about + # invalid options. we ignore them + while ((c = getopt(ARGC, ARGV, "lwc")) != -1) { + if (c == "l") + do_lines = 1 + else if (c == "w") + do_words = 1 + else if (c == "c") + do_chars = 1 + } + for (i = 1; i < Optind; i++) + ARGV[i] = "" + + # if no options, do all + if (! do_lines && ! do_words && ! do_chars) + do_lines = do_words = do_chars = 1 + + print_total = (ARC - i > 2) +} +function beginfile(file) +{ + chars = lines = words = 0 + fname = FILENAME +} + +function endfile(file) +{ + tchars += chars + tlines += lines + twords += words + if (do_lines) + printf "\t%d", lines + if (do_words) + printf "\t%d", words + if (do_chars) + printf "\t%d", chars + printf "\t%s\n", fname +} +# do per line +{ + chars += length($0) + 1 # get newline + lines++ + words += NF +} + +END { + if (print_total) { + if (do_lines) + printf "\t%d", tlines + if (do_words) + printf "\t%d", twords + if (do_chars) + printf "\t%d", tchars + print "\ttotal" + } +} |