zoukankan      html  css  js  c++  java
  • Linux之awk学习

     

    #这个实例计算在命令行上指定的某个文件中的每一列数字的总和。

    if (test $# = 0)then
        echo "you must apply a number."
        exit 1
    fi
    cat $1 | awk ' NR == 1 # awk < $1
            {
                nfields = NF #set nfields to number of fields in the record (NF)
            }

    {
        if($0 ~ /[^0-9. \t]/) #check each record to see if it contains any characters are not numbers.
        { #periods.spaces.or tabs
            printf "it is not a number record\t " $0
            printf "\n"

        }
        else
        {
            for(count = 1;count <= nfields;count ++) #for good records loop
            {
                printf "%10.2f",$count > "out"
                sum[count] += $count
                gtotal += $count
            }
            print "\n" > "out"
        }
    }
    END {

            for(count = 1;count <= nfields;count++)
            {
                printf " -------" > "out"
            }
            print "\n" > "out"
            for(count = 1;count <= nfields;count++)
            {
                printf"%10.2f",sum[count] > "out"
            }
            print "\n\n Grand Total" gtotal "\n" > "out"
         }
    '

  • 相关阅读:
    PHP使用Redis的GEO地理信息类型
    Redis长短链接的区别
    Linux之ln文件创建链接
    xml与json格式互转
    爬虫实例:唐诗宋词爬虫
    爬虫实例:天猫商品评论爬虫
    爬虫实例:饿了么爬虫
    爬虫实例:中国日报高频词汇爬虫
    爬虫实例:今日头条爬虫
    特殊类型的列表切片
  • 原文地址:https://www.cnblogs.com/benshan/p/2585399.html
Copyright © 2011-2022 走看看