Unix Shell范例精解---AWK练习(上)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unix Shell范例精解---AWK练习(上)相关的知识,希望对你有一定的参考价值。
Unix Shell范例精解第六章---AWK实用程序中有六个练习题,从简单到复杂,目前做了前面3道题目,后面的3题感觉做起来很吃力,关于循环、数组、函数的题目,不知道要怎么做。AWK练习1:练习1主要练习的是模式为正则表达式的操作,有些题目可以用书里后面讲解的内容解答
[kevin.tao@cws76 Ex_6.001-6.054]$ cat lab3.data
Mike Harrington:(510) 548-1278:250:100:175
Christian Dobbins:(408) 538-2358:155:90:201
Susan Dalsass:(206) 654-6279:250:60:50
Archie McNichol:(206) 548-1348:250:100:175
Jody Savage:(206) 548-1278:15:188:150
Guy Quigley:(916) 343-6410:250:100:175
Dan Savage:(406) 298-7744:450:300:275
Nancy McNeil:(206) 548-1278:250:80:75
John Goldenrod:(916) 348-4278:250:100:175
Chet Main:(510) 548-5258:50:95:135
Tom Savage:(408) 926-3456:250:168:200
Elizabeth Stachelin:(916) 440-1763:175:75:300
该数据库包含姓名、电话号码、以及在最近3个月中的竞选捐款数额
1.打印所有的电话号码
awk -F ‘[: ]‘ ‘print $3,$4 ‘ lab3.data #设置字段分隔符为冒号和空格
2.打印Dan的电话号码
awk -F ‘[: ]‘ ‘/^Dan/print $3 $4 ‘ lab3.data #模式匹配Dan加上相应的动作
3.打印Susan的姓名和电话号码
awk -F ‘[: ]‘ ‘/^Susan/print $1,$2, $3 $4 ‘ lab3.data
4.打印所有姓以D开头的姓氏
awk -F ‘[: ]‘ ‘$2~/^D/print $2‘ lab3.data #$x ~ /xxx/这种方式很常用
5.打印所有以C或E开头的名字
awk ‘/^[CE]/print $1‘ lab3.data
6.打印所有只包含4个字符的名字
awk ‘/^....\>/print $1‘ lab3.data #这种虽然结果是对的,但是不严谨
awk -F ‘[: ]‘ ‘$1~/^[a-zA-Z]4$/print $1‘ lab3.data
awk -F ‘[: ]‘ ‘if(length($1)==4)print $1‘ lab3.data
7.打印所有区号为916的人的名字
awk -F ‘[: ]‘ ‘$3=="(916)"print $1‘ lab3.data
awk -F ‘[: ]‘ ‘$3~/916/print $1‘ lab3.data
8.打印Mike的资助金额,每一个值要使用美元符号开头,例如“$250$100$175”
awk -F ‘[: ]‘ ‘/^Mike/print "$"$5"$"$6"$"$7‘ lab3.data
awk -F ‘[: ]‘ ‘$1~/Mike$/print "$"$5"$"$6"$"$7‘ lab3.data
9.打印所有姓,后面跟着一个逗号和名
awk -F ‘[: ]‘ ‘print $2","$1‘ lab3.data
10.编写一个名为facts的脚本,并完成下面的工作:
a.打印所有姓氏为Savage的人的全名和电话号码
b.打印Chet的资助金额
c.打印所有在第一个月资助了250美元的人
脚本内容:
#/usr/bin/awk
#打印所有姓氏为Savage的人的全名和电话号码
$2~/Savage$/print $1,$2,$3,$4
#打印Chet的资助金额
$1~/Chet$/print $5,$6,$7
#打印所有第一个月资助250美元的人
$5~/250$/print $1,$2
#执行脚本时要改变FS的值
AWK 练习2 :主要练习模式为比较表达式、关系运算符...等操作
[kevin.tao@cws76 Ex_6.055-6.089]$ cat lab4.data
Mike Harrington:(510) 548-1278:250:100:175
Christian Dobbins:(408) 538-2358:155:90:201
Susan Dalsass:(206) 654-6279:250:60:50
Archie McNichol:(206) 548-1348:250:100:175
Jody Savage:(206) 548-1278:15:188:150
Guy Quigley:(916) 343-6410:250:100:175
Dan Savage:(406) 298-7744:450:300:275
Nancy McNeil:(206) 548-1278:250:80:75
John Goldenrod:(916) 348-4278:250:100:175
Chet Main:(510) 548-5258:50:95:135
Tom Savage:(408) 926-3456:250:168:200
Elizabeth Stachelin:(916) 440-1763:175:75:300
该数据库包含姓名、电话号码、以及在最近3个月中的竞选捐款数额
1.打印在第二个月捐款超过100美元的人的姓名和电话号码
awk -F ‘[: ]‘ ‘$6>100print $1,$2,$3 $4‘ lab4.data
2.打印在最后一个月捐款少于85美元的人的姓名和电话号码
awk -F ‘[: ]‘ ‘$7<85print $1,$2,$3 $4‘ lab4.data
3.打印第一个月捐款额在75-150美元之间的人
awk -F ‘[: ]‘ ‘$5>75 && $5<150print $1,$2‘ lab4.data
4.打印这三个月的捐款总额不超过800美元的人
awk -F ‘[: ]‘ ‘($5+$6+47)<800print $1,$2‘ lab4.data
awk -F ‘[: ]‘ ‘if(($5+$6+$7)<800)print $1,$2‘ lab4.data
5.打印月均捐款额大于200美元的人的姓名和电话号码
awk -F ‘[: ]‘ ‘($5+$6+$7)/3>200print $1,$2,$3 $4‘ lab4.data
awk -F ‘[: ]‘ ‘if(($5+$6+$7)/3>200)print $1,$2,$3 $4‘ lab4.data
6.打印不在916区的人的姓
awk -F ‘[: ]‘ ‘!($3=="(916)")print $2‘ lab4.data
7.打印每条记录,并在记录前加上记录号
awk ‘print NR,$0‘ lab4.data
8.打印每个人的姓名和捐款总额
awk -F ‘[: ]‘ ‘print $1,$2,$5+$6+$7‘ lab4.data
9.把Chet第二个月的捐款额加上10
awk -F ‘[: ]‘ ‘/^\<Chet\>/print $6+10‘ lab4.data
awk -F ‘[: ]‘ ‘/^\<Chet\>/$6=$6+10;print‘ lab4.data
10.把Nancy McNeil的名字改成Louise McInnes
awk -F ‘[: ]‘ ‘$1=="Nancy" && $2=="McNeil"$1="Louise";$2="McInnes";print $1,$2 ‘ lab4.data
AWK练习3 :主要练习变量,格式控制、BEGIN END 等操作
[kevin.tao@cws76 Ex_6.090-6.126]$ cat lab5.data
Mike Harrington:(510) 548-1278:250:100:175
Christian Dobbins:(408) 538-2358:155:90:201
Susan Dalsass:(206) 654-6279:250:60:50
Archie McNichol:(206) 548-1348:250:100:175
Jody Savage:(206) 548-1278:15:188:150
Guy Quigley:(916) 343-6410:250:100:175
Dan Savage:(406) 298-7744:450:300:275
Nancy McNeil:(206) 548-1278:250:80:75
John Goldenrod:(916) 348-4278:250:100:175
Chet Main:(510) 548-5258:50:95:135
Tom Savage:(408) 926-3456:250:168:200
Elizabeth Stachelin:(916) 440-1763:175:75:300
上面这个数据库记录内容包括姓名、电话号码和最近3个月得竞选捐款数额
试编写一个能产生如下输出的nawk脚本:
[kevin.tao@cws76 Ex_6.090-6.126]$ awk -f nawk.sc lab5.data
***CAMPAIGN 1998 CONTRIBUTIONS***
--------------------------------------------------------------------------------------
NAME PHONE Jan| Feb | Mar | Total Donated
--------------------------------------------------------------------------------------
Mike Harrington (510) 548-1278 250.00 100.00 175.00 525.00
Christian Dobbins (408) 538-2358 155.00 90.00 201.00 446.00
Susan Dalsass (206) 654-6279 250.00 60.00 50.00 360.00
Archie McNichol (206) 548-1348 250.00 100.00 175.00 525.00
Jody Savage (206) 548-1278 15.00 188.00 150.00 353.00
Guy Quigley (916) 343-6410 250.00 100.00 175.00 525.00
Dan Savage (406) 298-7744 450.00 300.00 275.00 1025.00
Nancy McNeil (206) 548-1278 250.00 80.00 75.00 405.00
John Goldenrod (916) 348-4278 250.00 100.00 175.00 525.00
Chet Main (510) 548-5258 50.00 95.00 135.00 280.00
Tom Savage (408) 926-3456 250.00 168.00 200.00 618.00
Elizabeth Stachelin (916) 440-1763 175.00 75.00 300.00 550.00
----------------------------------------------------------------------------------
SUMMARY
----------------------------------------------------------------------------------
The campaign received a total of 6137.00 for this quarter. #所有人的捐款总和
The average donation for the 12 contribuors was $511.42. #平均每人捐款多少
The highest contribution was $450.00. #单月最大捐款
The lowest contribution was $15.00. #单月最低捐款
书本里面字段之间是对齐的,写的脚本输出的格式没有很整齐,脚本内容如下:
[kevin.tao@cws76 Ex_6.090-6.126]$ cat nawk.sc
#/usr/bin/awk
BEGIN FS=":"
printf "%40s\n","***CAMPAIGN 1998 CONTRIBUTIONS***"
print "--------------------------------------------------------------------------------------"
printf "%-22s %-16s %7s| %7s | %7s | %8s","NAME","PHONE", "Jan", "Feb","Mar","Total Donated\n"
print "--------------------------------------------------------------------------------------"
total=0;sum=0;lc=0;highest=0;lowest
total=$3+$4+$5;sum=sum+total;lc=lc+1
highest=(highest > $3)? highest : $3
highest=(highest > $4)? highest : $4
highest=(highest > $5)? highest : $5
lowest=(lowest==0 || lowest > $3)? $3 : lowest
lowest=(lowest < $4)? lowest : $4
lowest=(lowest < $5)? lowest : $5
printf "%-22s %-16s %7.2f %7.2f %7.2f %8.2f\n",$1,$2,$3,$4,$5,total
END
printf "----------------------------------------------------------------------------------\n"
printf "\t\t\tSUMMARY\n"
printf "----------------------------------------------------------------------------------\n"
printf "The campaign received a total of %7.2f for this quarter.\n",sum
printf "The average donation for the 12 contribuors was $%.2f.\n",sum/lc #或者用sum/NR、sum/12
printf "The highest contribution was $%.2f.\n",highest
printf "The lowest contribution was $%.2f.\n",lowest
#total变量用于记录每个人3个月捐款总数,sum变量用来记录所有人的捐款总和,lc变量用来记录记录数目(sum/lc和sum/NR都等于sum/12 ),highest和lowest变量用来处理最大值和最小值。
总结:感觉awk学起来很困难,内容很多,书中后面的练习也做不出来,需要用到数组、函数、循环之类的功能,感觉awk脚本比bash脚本要难写。
以上是关于Unix Shell范例精解---AWK练习(上)的主要内容,如果未能解决你的问题,请参考以下文章