wc.awk

来自「gawk: linux下资料处理工具」· AWK 代码 · 共 70 行

AWK
70
字号
# wc.awk --- count lines, words, characters## Arnold Robbins, arnold@skeeve.com, Public Domain# May 1993# Options:#    -l    only count lines#    -w    only count words#    -c    only count characters## Default is to count lines, words, characters## Requires getopt and file transition library functionsBEGIN {    # 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"    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?