shell编程系列18--文本处理三剑客之awk动作中的条件及if/while/do while/for循环语句
条件语句
if(条件表达式)
动作1
else if(条件表达式)
动作2
else
动作3
循环语句:
while循环:
while(条件表达式)
动作
do while循环:
do
动作
while(条件表达式)
for循环:
for(初始化计数器;计数器测试;计数器变更)
动作
、以:为分隔符,只打印/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< root
UID< bin
UID< daemon
UID< adm
UID< lp
UID< sync
UID< shutdown
UID< halt
UID< mail
UID< operator
UID< games
UID< ftp
<UID< nobody
UID> systemd-network
<UID< dbus
UID> polkitd
<UID< sshd
<UID< postfix
UID> ajie
UID> chrony
UID> deploy
UID> nginx
[root@localhost shell]# cat if.awk
BEGIN{
FS=":"
}
{
if($<)
{
printf "%-20s%-20s%-10d\n","UID<50",$,$
}
else if ($> && $<)
{
printf "%-20s%-20s%-10d\n","50<UID<100",$,$
}
else
{
printf "%-20s%-20s%-10d\n","UID>100",$,$
}
}
、计算下列每个同学的平均分数,并且只打印平均分数大于90的同学姓名和分数信息
Allen
Mike
Zhang
Jerry
Han
Li
# 算出平均成绩
[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 91.00
Mike 93.50
Zhang 83.25
Jerry 83.75
Han 86.25
Li 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 91.00
Mike 93.50
Li 91.00
、计算1++++...+100的和,请使用while、do while、for三种循环方式实现
通过读取文件的方式载入awk的条件
# while循环
[root@localhost shell]# cat add_while.awk
BEGIN{
while(i<=)
{
# 一个变量不赋值,默认为0或者空
sum+=i
i++
}
print sum
}
# do while循环
[root@localhost shell]# awk -f add_dowhile.awk
[root@localhost shell]# cat add_dowhile.awk
BEGIN{
do {
sum+=i
i++
}while(i<=)
print sum
}
# for循环
[root@localhost shell]# awk -f add_for.awk
[root@localhost shell]# cat add_for.awk
BEGIN{
for(i=;i<=;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
小于50的UID bin
小于50的UID daemon
小于50的UID adm
小于50的UID lp
小于50的UID sync
小于50的UID shutdown
小于50的UID halt
小于50的UID mail
小于50的UID operator
小于50的UID games
小于50的UID ftp
大于50并且小于100的UID nobody
大于100的UID systemd-network
大于50并且小于100的UID dbus
大于100的UID polkitd
大于50并且小于100的UID sshd
大于50并且小于100的UID postfix
大于100的UID ajie
大于100的UID chrony
大于100的UID deploy
大于100的UID nginx
# awk 通过-f参数读取文件的方式读取条件进行输出
[root@localhost shell]# cat scripts.awk
BEGIN{
FS=":"
}
{
if($<)
{
printf "%-20s%-30s%-5d\n","UID<50",$,$
}
else if ($> && $<)
{
printf "%-20s%-30s%-5d\n","50<UID<100",$,$
}
else
{
printf "%-20s%-30s%-5d\n","UID>100",$,$
}
}
[root@localhost shell]# awk -f scripts.awk passwd
UID< root
UID< bin
UID< daemon
UID< adm
UID< lp
UID< sync
UID< shutdown
UID< halt
UID< mail
UID< operator
UID< games
UID< ftp
<UID< nobody
UID> systemd-network
<UID< dbus
UID> polkitd
<UID< sshd
<UID< postfix
UID> ajie
UID> chrony
UID> deploy
UID> nginx
[root@localhost shell]#
循环语句-do while
do while
do
动作
while(条件表达式)
循环语句-for
for
for(初始化计数器;测试计数器;计数器变更)
动作
# while循环
[root@localhost shell]# cat while.awk
BEGIN{
while(i<=)
{
sum+=i
i++
}
print sum
}
[root@localhost shell]# awk -f while.awk
# for循环
[root@localhost shell]# cat for.awk
BEGIN{
for(i=;i<=;i++)
{
sum+=i
}
print sum
}
[root@localhost shell]# awk -f for.awk
# do while循环
[root@localhost shell]# cat dowhile.awk
BEGIN{
do
{
sum+=i
i++
}while(i<=)
print sum
}
[root@localhost shell]# awk -f dowhile.awk
[root@localhost shell]# cat student.txt
Name chinese english math physical average
Allen
Mike
Zhang
Jerry
Han
Li
# 打印平均分大于90的人,并计算出各科总分
[root@localhost shell]# cat student.txt
Allen
Mike
Zhang
Jerry
Han
Li
[root@localhost shell]# cat student.awk
BEGIN{
printf "%-10s%-10s%-10s%-10s%-10s%-10s\n","Name","Chinese","English","Math","Physical","Average"
}
{
total=$+$+$+$
avg=total/
if(avg>)
{
printf "%-10s%-10d%-10d%-10d%-10d%-0.2f\n",$,$,$,$,$,avg
# 将每一科的成绩累加起来保存在一个变量中
score_chinese+=$
score_english+=$
score_math+=$
score_physical=$
}
}
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 91.00
Mike 93.50
Li 91.00
Name