Unix Shell paradigm fine solution --- AWK practice (on)

Unix Shell example Explained Chapter VI --- AWK utility has six exercises, from simple to complex, currently doing a front three questions, 3 topic feeling behind it is very difficult to do, about loops, arrays, functions, the problem, do not know how to do.

AWK Exercise 1: Exercise 1 main exercise is the regular operating mode of expression, some topics can be used later to explain the contents of the book answer

[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 

The database contains names, phone numbers, and campaign contributions in the last three months of

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 Exercise 2: Comparison of expression of the main exercise mode, and other operations relational operators ...

[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

The database contains names, phone numbers, and campaign contributions in the last three months of

1.打印在第二个月捐款超过100美元的人的姓名和电话号码
awk -F '[: ]' '$6>100{print $1,$2,$3 $4}' lab4.data

2.打印在最后一个月捐款少于85美元的人的姓名和电话号码
awk -F '[: ]' '$7<85{print $1,$2,$3 $4}' lab4.data 

3.打印第一个月捐款额在75-150美元之间的人
awk -F '[: ]' '$5>75 && $5<150{print $1,$2}' lab4.data 

4.打印这三个月的捐款总额不超过800美元的人
awk -F '[: ]' '($5+$6+47)<800{print $1,$2}' lab4.data 
awk -F '[: ]' '{if(($5+$6+$7)<800){print $1,$2}}' lab4.data 

5.打印月均捐款额大于200美元的人的姓名和电话号码
awk -F '[: ]' '($5+$6+$7)/3>200{print $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 Exercise 3: the main exercise variable format control, BEGIN END other operations

[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 

The above database records including name, telephone number and the last three months was the amount of campaign contributions from
trial can write a script nawk produces the following output:

[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.                                               #单月最低捐款

Is aligned between the books inside the field, the format to write the script output is not very neat, the script reads as follows:

[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变量用来处理最大值和最小值。

Summary: awk feel very difficult to learn, a lot of content, the back of the book can not do this exercise, the need to use arrays, functions, loop functions like, feel awk scripts to be difficult to write than bash script.

Guess you like

Origin blog.51cto.com/pkimin3/2427025