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