diff options
author | sheldonh <sheldonh@FreeBSD.org> | 2000-08-15 10:02:07 +0000 |
---|---|---|
committer | sheldonh <sheldonh@FreeBSD.org> | 2000-08-15 10:02:07 +0000 |
commit | 4e5281d00b8fa7447d70020d36660157e7e43626 (patch) | |
tree | 88d7fea3d791af620456e3cd42ff2bf1c4229af2 /contrib/awk/awklib/eg/prog/wc.awk | |
parent | 946d89ae2629e07c7d4735eac8d1f5ac2263ce58 (diff) | |
download | FreeBSD-src-4e5281d00b8fa7447d70020d36660157e7e43626.zip FreeBSD-src-4e5281d00b8fa7447d70020d36660157e7e43626.tar.gz |
Update vendor branch to gawk-3.0.6.
Diffstat (limited to 'contrib/awk/awklib/eg/prog/wc.awk')
-rw-r--r-- | contrib/awk/awklib/eg/prog/wc.awk | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/contrib/awk/awklib/eg/prog/wc.awk b/contrib/awk/awklib/eg/prog/wc.awk new file mode 100644 index 0000000..56aab42 --- /dev/null +++ b/contrib/awk/awklib/eg/prog/wc.awk @@ -0,0 +1,67 @@ +# wc.awk --- count lines, words, characters +# Arnold Robbins, arnold@gnu.org, 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 = (ARGC - 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" + } +} |