Uinx Shell by Example,3rd Edition第5,6章 awk 章节的答案

本文提供了Fedora 8系统中使用awk处理电话簿数据的示例,包括打印所有电话号码、特定人员信息、特定区域代码等。通过一系列练习题和解答,介绍了awk在处理姓名、电话、捐赠金额等方面的功能,如筛选特定姓氏、字符长度条件、贡献金额范围等,并展示了如何编写awk脚本来实现特定任务,如统计捐赠总额、修改记录等。
摘要由CSDN通过智能技术生成

#说明下我系统是fedora8。 awk实际是链接到gawk,也就是说调用awk实际调用的是gawk
文件名datafile内容如下:
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 above contains the names, phone numbers, and money #contributions to the party campaign for the past three months.
##############
Chapter 5:
##############
#Practice:
 1: Print all the phone numbers.
 2: Print Dan's phone number.
 3: Print Susan's name and phone number.
 4: Print all last names beginning with D.
 5: Print all first names beginning with either a C or E.
 6: Print all first names containing only four characters.
 7: Print the first names of all those in the 916 area code.
 8: Print Mike's campaign contributions. Each value should be printed with a leading
    dollar sign; e.g., $250 $100 $175.
 9: Print last names followed by a comma and the first name.
10: Write an awk script called facts that:
      a. Prints full names and phone numbers for the Savages.
      b. Prints Chet's contributions.
      c. Prints all those who contributed $250 the first month.


Answers:
#1. awk -F: '{print $2}' datafile
#2. awk -F: '/Dan/{print $2}' datafile
#3. awk -F: '/Susan/{print $1,$2}' datafile
#4. awk '$2 ~ /^D[a-z]*/ {print}' datafile
#5. awk '$1 ~ /^[C-E][a-z]*/ {print}' datafile
#6. awk 'length($1)==4 {print}' datafile
#7. awk -F'[: ]' '$3 ~/(916)/{print}' datafile
#8. awk -F: '/Mike/{printf "$%s $%s $%s/n",$3,$4,$5}' datafile
#9. 
#方法1: awk -F'[: ]' '{printf "%s,%s:%s %s:%s:%s:%s/n",$1,$2,$3,$4,$5,$6,$7}' datafile
#方法2: awk -F'[: ]' '{sub(substr($0,1,index($0," ")),$1",",$0) ;print $0 }' datafile

#10.[chlaws@mylinux awk]$ cat facts
#This is awk script
#Name:facts
BEGIN{FS=":"}
/Savage/{print $1,$2}
/Chet/{s=$3+$4+$5;print "Chet contribution:", s}
$3~/250/{print}

[chlaws@mylinux awk]$  awk -f facts datafile





##############
#Chapter6:
##############
Practices:
    1:     Print the first and last names of those who contributed over $100 in the first month.
    2:     Print the names and phone numbers of those who contributed less than $60 in the first month.
    3:     Print those who contributed between $90 and $150 in the third month.
    4:     Print those who contributed more than $800 over the three-month period.
    5:     Print the names and phone numbers of those with an average monthly contribution greater than $150.
    6:     Print the first name of those not in the 916 area code.
    7:    Print each record preceded by the number of the record.
    8:    Print the name and total contribution of each person.
    9:    Add $10 to Elizabeth's second contribution.
   10:     Change Nancy McNeil's name to Louise McInnes.

Answers:
#1.awk -F: '$3 > 100 {print $1}' datafile
#2.awk -F: '$3 < 60  {print $1,$2}' datafile
#3.awk -F: '($5>=90)&&($5<=150){print $1,$2}' datafile
#4.awk -F: '($3+$4+$5)>800 {print}' datafile
#5.awk -F: '($3+$4+$5)/3>150 {print}' datafile
#6.awk -F'[: ]' '$3!~/(916)/{print}' datafile
#7.awk '{print NR,$0}' datafile
#8.awk -F: '{print $1,$3+$4+$5}' datafile
#9.
#方法1:awk -F: '/Elizabeth/{printf "%s:%s:%s:%s:%s/n",$1,$2,$3,$4+10,$5}' datafile
#方法2:awk -F: '/Elizabeth/{gsub($4,$4+10,$0);print $0}' datafile
#10.
#方法1:awk -F: '/Nancy McNei/{printf "%s:%s:%s:%s:%s/n","Louise Mclnnes",$2,$3,$4,$5}' datafile
#方法2:awk -F: 'BEGIN{OFS=":"}/Nancy McNei/{sub("Nancy McNei","Louise Mclnnes",$0);print}' datafile

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值