欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

awk练习笔记

程序员文章站 2023-04-05 20:58:33
题目数据如下: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201Susan Dalsass:(206) 654-6279:250:60:50Archie McNichol:(2 ......

题目数据如下:

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: '$4>100{print $1}' lab3.data
2.打印在最后一个月捐款少于85美元的人的姓名和电话号码
awk -f: '$5<85{print $1,$2}' lab3.data
3.打印第一个月捐款额在75~150美元之间的人。
awk -f: '$5>=75 && $5<=150 {print $1}' lab3.data
4.打印这3个月的捐款总额不超过800美元的人。
awk -f: '$3+$4+$5 <= 800 {print $1}' lab3.data
5.打印月均捐款额大于200美元的人的姓名和电话号码。
awk -f: '$3+$4+$5 > 600{print $1,$2}' lab3.data
6.打印不在916区的人的姓。
awk -f"[ :]" '$3 !~ "916"{print $2}' lab3.data
7.打印每条记录,并在记录前加上其记录号。
awk '{print nr,$0}' lab3.data
8.打印每个人的姓名和捐款总额。
awk -f: '{print $1,$3+$4+$5}' lab3.data
9.把chet第二个月的捐款额加上10
awk -f: '/chet/{$4=$4+10} {print $0}' lab3.data
10.把 nancy mcneil的名字改成louise mcinnes
awk -f: '/nancy mcneil/{$1="louise mclnnes"} {print}' lab3.data

 11.写出一个awk脚本输出以下信息

 

                       ***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 conrtibutors was $511.42.
the highest contribution was $450.00.
the lowest contribution was $15.00.
$ cat lab3.awk 
#!/usr/bin/awk -f

begin{
    fs=":"
    print "                       ***campaign 1998 contributions***"
    print "--------------------------------------------------------------------------------"
    print "name                      phone            jan  |  feb  |  mar  |  total donated"
    print "--------------------------------------------------------------------------------"
    min=1000.0
    max=0
    total=0
}
{
    total+=$3+$4+$5
    if($3<min)
        min=$3
    if($4<min)
        min=$4
    if($5<min)
        min=$5
    if($3>max)
        max=$3
    if($4>max)
        max=$4
    if($5>max)
        max=$5
    printf "%-20s%14s        %-6.2f  %-6.2f  %-6.2f  %-6.2f\n",$1,$2,$3,$4,$5,$3+$4+$5 
}
end{
    print "--------------------------------------------------------------------------------"
    print "                              summary                                           "
    print "--------------------------------------------------------------------------------"
    printf "the campaign received a total of $%6.2f for this quarter.\n",total
    printf "the average donation for the 12 conrtibutors was $%5.2f.\n",total/12.0
    printf "the highest contribution was $%5.2f.\n",max
    printf "the lowest contribution was $%4.2f.\n",min
}