shell编程系列17--文本处理三剑客之awk动作中的表达式用法

shell编程系列17--文本处理三剑客之awk动作中的表达式用法


awk动作表达式中的算数运算符

awk动作中的表达式用法总结:

    运算符        含义
    +-*/%^或**        乘方
    ++x        在返回x变量之前,x变量加1
    x++        在返回x变量之后,x变量加1
    --x        在返回x变量之前,x变量减1
    x--        在返回x变量之后,x变量减1

    1、使用awk计算/etc/services中的空白行数量
        awk 'BEGIN{sum=0}/^$/{++sum}END{print sum}' /etc/services
    2、计算学生课程分数平均值,学生课程文件内容如下:
        Allen    80    90    96    98
        Mike    93    98    92    91
        Zhang    78    76    87    92
        Jerry    86    89    68    92
        Han    85    95    75    90
        Li    78    88    98    100

        # 代码如下:
        [root@localhost shell]# awk 'BEGIN{printf "%-20s%-20s%-20s%-20s%-20s%-20s\n","Name","Chinese","English","Math","Physical","Average"}{sum=$2+$3+$4+$5;avg=sum/4}{printf "%-20s%-20d%-20d%-20d%-20d%-0.2f\n",$1,$2,$3,$4,$5,avg}' student.txt 
        Name                Chinese             English             Math                Physical            Average             
        Allen               80                  90                  96                  98                  91.00
        Mike                93                  98                  92                  91                  93.50
        Zhang               78                  76                  87                  92                  83.25
        Jerry               86                  89                  68                  92                  83.75
        Han                 85                  95                  75                  90                  86.25
        Li                  78                  88                  98                  100                 91.00


[root@localhost shell]# awk 'BEGIN{num1=20;num2+=num1;print num1,num2}'
20 20
[root@localhost shell]# awk 'BEGIN{num1=20;num2=30;print num1+num2}'
50
[root@localhost shell]# awk 'BEGIN{num1=20;num2=30;print num1-num2}'
-10
[root@localhost shell]# awk 'BEGIN{num1=20;num2=30;print num1*num2}'
600
[root@localhost shell]# awk 'BEGIN{num1=20;num2=30;print num1/num2}'
0.666667
# 保留小数后两位
[root@localhost shell]# awk 'BEGIN{num1=20;num2=30;printf "%0.2f\n",num1/num2}'
0.67

[root@localhost shell]# awk 'BEGIN{num1=20;num2=3;printf "%0.2f\n",num1**num2}'
8000.00
# 打印
[root@localhost shell]# awk 'BEGIN{num1=20;num2=3;printf "%0.2f\n",num1^num2}'
8000.00

# 
[root@localhost shell]# awk 'BEGIN{x=20;y=x++;print x,y}'
21 20
[root@localhost shell]# awk 'BEGIN{x=20;y=++x;print x,y}'
21 21
[root@localhost shell]# awk 'BEGIN{x=20;y=--x;print x,y}'
19 19
[root@localhost shell]# awk 'BEGIN{x=20;y=x--;print x,y}'
19 20


# 输出平均值
[root@localhost shell]# cat student.txt 
Allen    80    90    96    98
Mike    93    98    92    91
Zhang    78    76    87    92
Jerry    86    89    68    92
Han    85    95    75    90
Li    78    88    98    100
[root@localhost shell]# awk '{total=$2+$3+$4+$5;AVG=total/4}{printf "%-8s%-5d%-5d%-5d%-8d%0.2f\n",$1,$2,$3,$4,$5,AVG}' student.txt 
Allen   80   90   96   98      91.00
Mike    93   98   92   91      93.50
Zhang   78   76   87   92      83.25
Jerry   86   89   68   92      83.75
Han     85   95   75   90      86.25
Li      78   88   98   100     91.00
# 加上标题并格式化输出
[root@localhost shell]# awk 'BEGIN{printf "%-10s%-10s%-10s%-10s%-10s%-10s\n","name","Yuwen","math","English","Pysical","Average"}{total=$2+$3+$4+$5;AVG=total/4}{printf "%-10s%-10d%-10d%-10d%-10d%-10.2f\n",$1,$2,$3,$4,$5,AVG}' student.txt
name      Yuwen     math      English   Pysical   Average   
Allen     80        90        96        98        91.00     
Mike      93        98        92        91        93.50     
Zhang     78        76        87        92        83.25     
Jerry     86        89        68        92        83.75     
Han       85        95        75        90        86.25     
Li        78        88        98        100       91.00 

 

posted @ 2019-06-06 14:28  reblue520  阅读(232)  评论(0编辑  收藏  举报