awk练习笔记

2019-04-21 07:04:54来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

题目数据如下:

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
}

 


原文链接:https://www.cnblogs.com/jackis-tang/p/10745015.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:centos配置静态ip地址

下一篇:nginx配置默认首页(index.htnl index.htm)全流程(包含遇到问