shell编程系列18--文本处理三剑客之awk动作中的条件及if/while/do while/for循环语句

shell编程系列18--文本处理三剑客之awk动作中的条件及if/while/do while/for循环语句
条件语句
if(条件表达式) 动作1 else if(条件表达式) 动作2 else 动作3 循环语句: while循环: while(条件表达式) 动作 do while循环: do 动作 while(条件表达式) for循环: for(初始化计数器;计数器测试;计数器变更) 动作 1、以:为分隔符,只打印/etc/passwd中第3个字段的数值在50-100范围内的行信息 # 简单运算符处理 awk 'BEGIN{FS=":"}{if($3>50 && $3<100) print $0}' passwd # if 条件处理 # 输出 以 UID 以50位为分界点的用户,-f 将条件表达式写到文本中读取 [root@localhost shell]# awk -f if.awk passwd UID<50 root 0 UID<50 bin 1 UID<50 daemon 2 UID<50 adm 3 UID<50 lp 4 UID<50 sync 5 UID<50 shutdown 6 UID<50 halt 7 UID<50 mail 8 UID<50 operator 11 UID<50 games 12 UID<50 ftp 14 50<UID<100 nobody 99 UID>100 systemd-network 192 50<UID<100 dbus 81 UID>100 polkitd 999 50<UID<100 sshd 74 50<UID<100 postfix 89 UID>100 ajie 1000 UID>100 chrony 998 UID>100 deploy 1001 UID>100 nginx 997 [root@localhost shell]# cat if.awk BEGIN{ FS=":" } { if($3<50) { printf "%-20s%-20s%-10d\n","UID<50",$1,$3 } else if ($3>50 && $3<100) { printf "%-20s%-20s%-10d\n","50<UID<100",$1,$3 } else { printf "%-20s%-20s%-10d\n","UID>100",$1,$3 } } 2、计算下列每个同学的平均分数,并且只打印平均分数大于90的同学姓名和分数信息 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 # 加入条件判断,如果平均分数大于90才打印 [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}{if(avg>90) 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 Li 78 88 98 100 91.00 3、计算1+2+3+4+...+100的和,请使用while、do while、for三种循环方式实现 通过读取文件的方式载入awk的条件 # while循环 [root@localhost shell]# cat add_while.awk BEGIN{ while(i<=100) { # 一个变量不赋值,默认为0或者空 sum+=i i++ } print sum } # do while循环 [root@localhost shell]# awk -f add_dowhile.awk 5050 [root@localhost shell]# cat add_dowhile.awk BEGIN{ do { sum+=i i++ }while(i<=100) print sum } # for循环 [root@localhost shell]# awk -f add_for.awk 5050 [root@localhost shell]# cat add_for.awk BEGIN{ for(i=0;i<=100;i++) { sum+=i } print sum } # uid在50和100之间 [root@localhost shell]# awk 'BEGIN{FS=":"}{if($3>50 && $3<100) print $0}' passwd # uid小于50或者大于100 [root@localhost shell]# awk 'BEGIN{FS=":"}{if($3<50 || $3>100) print $0}' passwd [root@localhost shell]# awk 'BEGIN{FS=":"}{if($3<50){printf "%-30s%-30s%-15d\n","小于50的UID",$1,$3} else if($3>50 && $3<100) {printf "%-30s%-30s%-15d\n","大于50并且小于100的UID",$1,$3} else {printf "%-30s%-30s%-15d\n","大于100的UID",$1,$3}}' passwd 小于50的UID root 0 小于50的UID bin 1 小于50的UID daemon 2 小于50的UID adm 3 小于50的UID lp 4 小于50的UID sync 5 小于50的UID shutdown 6 小于50的UID halt 7 小于50的UID mail 8 小于50的UID operator 11 小于50的UID games 12 小于50的UID ftp 14 大于50并且小于100的UID nobody 99 大于100的UID systemd-network 192 大于50并且小于100的UID dbus 81 大于100的UID polkitd 999 大于50并且小于100的UID sshd 74 大于50并且小于100的UID postfix 89 大于100的UID ajie 1000 大于100的UID chrony 998 大于100的UID deploy 1001 大于100的UID nginx 997 # awk 通过-f参数读取文件的方式读取条件进行输出 [root@localhost shell]# cat scripts.awk BEGIN{ FS=":" } { if($3<50) { printf "%-20s%-30s%-5d\n","UID<50",$1,$3 } else if ($3>50 && $3<100) { printf "%-20s%-30s%-5d\n","50<UID<100",$1,$3 } else { printf "%-20s%-30s%-5d\n","UID>100",$1,$3 } } [root@localhost shell]# awk -f scripts.awk passwd UID<50 root 0 UID<50 bin 1 UID<50 daemon 2 UID<50 adm 3 UID<50 lp 4 UID<50 sync 5 UID<50 shutdown 6 UID<50 halt 7 UID<50 mail 8 UID<50 operator 11 UID<50 games 12 UID<50 ftp 14 50<UID<100 nobody 99 UID>100 systemd-network 192 50<UID<100 dbus 81 UID>100 polkitd 999 50<UID<100 sshd 74 50<UID<100 postfix 89 UID>100 ajie 1000 UID>100 chrony 998 UID>100 deploy 1001 UID>100 nginx 997 [root@localhost shell]# 循环语句-do while do while do 动作 while(条件表达式) 循环语句-for for for(初始化计数器;测试计数器;计数器变更) 动作 # while循环 [root@localhost shell]# cat while.awk BEGIN{ while(i<=100) { sum+=i i++ } print sum } [root@localhost shell]# awk -f while.awk 5050 # for循环 [root@localhost shell]# cat for.awk BEGIN{ for(i=0;i<=100;i++) { sum+=i } print sum } [root@localhost shell]# awk -f for.awk 5050 # do while循环 [root@localhost shell]# cat dowhile.awk BEGIN{ do { sum+=i i++ }while(i<=100) print sum } [root@localhost shell]# awk -f dowhile.awk 5050 [root@localhost shell]# cat student.txt Name chinese english math physical average 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 # 打印平均分大于90的人,并计算出各科总分 [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]# cat student.awk BEGIN{ printf "%-10s%-10s%-10s%-10s%-10s%-10s\n","Name","Chinese","English","Math","Physical","Average" } { total=$2+$3+$4+$5 avg=total/4 if(avg>90) { printf "%-10s%-10d%-10d%-10d%-10d%-0.2f\n",$1,$2,$3,$4,$5,avg # 将每一科的成绩累加起来保存在一个变量中 score_chinese+=$2 score_english+=$3 score_math+=$4 score_physical=$5 } } END{ printf "%-10s%-10d%-10d%-10d%-10d\n","Name",score_chinese,score_english,score_math,score_physical } [root@localhost shell]# awk -f student.awk student.txt Name Chinese English Math Physical Average Allen 80 90 96 98 91.00 Mike 93 98 92 91 93.50 Li 78 88 98 100 91.00 Name 251 276 286 100

 

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