diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2020-10-04 12:27:34 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2020-10-04 12:27:34 +0300 |
commit | 0ee9fa0f6ef9ac84a254ebf4068d15a474e0605b (patch) | |
tree | 66af0fe7a66b4a56fdfc27151d9eac8843ca2240 /awklib/eg/prog | |
parent | d8e0c7f7f2d397484f11df9d3b4805d5a343a1b6 (diff) | |
download | egawk-0ee9fa0f6ef9ac84a254ebf4068d15a474e0605b.tar.gz egawk-0ee9fa0f6ef9ac84a254ebf4068d15a474e0605b.tar.bz2 egawk-0ee9fa0f6ef9ac84a254ebf4068d15a474e0605b.zip |
Update wc example to modern POSIX.
Diffstat (limited to 'awklib/eg/prog')
-rw-r--r-- | awklib/eg/prog/wc.awk | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/awklib/eg/prog/wc.awk b/awklib/eg/prog/wc.awk index c46d0984..46804737 100644 --- a/awklib/eg/prog/wc.awk +++ b/awklib/eg/prog/wc.awk @@ -2,39 +2,46 @@ # # Arnold Robbins, arnold@skeeve.com, Public Domain # May 1993 +# Revised September 2020 # Options: # -l only count lines # -w only count words -# -c only count characters +# -c only count bytes +# -m only count characters # -# Default is to count lines, words, characters +# Default is to count lines, words, bytes # # Requires getopt() and file transition library functions +# Requires mbs extension from gawkextlib + +@load "mbs" BEGIN { # let getopt() print a message about # invalid options. we ignore them - while ((c = getopt(ARGC, ARGV, "lwc")) != -1) { + while ((c = getopt(ARGC, ARGV, "lwcm")) != -1) { if (c == "l") do_lines = 1 else if (c == "w") do_words = 1 else if (c == "c") + do_bytes = 1 + else if (c == "m") 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 + # if no options, do lines, words, bytes + if (! do_lines && ! do_words && ! do_chars && ! do_bytes) + do_lines = do_words = do_bytes = 1 print_total = (ARGC - i > 1) } function beginfile(file) { - lines = words = chars = 0 + lines = words = chars = bytes = 0 fname = FILENAME } function endfile(file) @@ -42,17 +49,21 @@ function endfile(file) tlines += lines twords += words tchars += chars + tbytes += bytes if (do_lines) printf "\t%d", lines if (do_words) printf "\t%d", words if (do_chars) printf "\t%d", chars + if (do_bytes) + printf "\t%d", bytes printf "\t%s\n", fname } # do per line { chars += length($0) + 1 # get newline + bytes += mbs_length($0) + 1 lines++ words += NF } @@ -64,6 +75,8 @@ END { printf "\t%d", twords if (do_chars) printf "\t%d", tchars + if (do_bytes) + printf "\t%d", tbytes print "\ttotal" } } |