AWK使用练习

环境RedHat Linux 9 + VWWare 8.0 + SSH 3.2.9

任务

下面的文本中包含名字、电话和为党派运动捐款的数额。名字:电话:1月捐款情况:2月捐款情况:3月捐款情况Mike Harrington :(510) 548-1278:250:100:175Christian Dobbins:(408) 538-2358:155:90:201Susan Dalsass:(206) 654-6279:250:60:50Archie McNichol:(206) 548-1348:250:100:175Jody Savage: (206) 548-1278:15:188:150Guy Quigley:(916) 343-6410:250: 100:175Dan Savage:(406) 298-7744:450:300:275NancyMcNeil:(206) 548-1278:250:80:75John Goldenrod:(916) 348-4278:250:100:175Chet Main:(510) 548-5258:50:95:135Tom Savage:(408) 926-3456:250:168:200Elizabeth Stachelin:(916) 440-1763:175:75:300使用你能用到的任何linux命令脚本产生一个如下的报告,,注意,报告中的summery包含对于捐款情况的一些统计信息。

解决

#!/bin/bash #filename:test.sh sourcename=$1 echo “$sourcename” sed -i “s/: /:/g” sourcename echo ” ***FIRST QUARTERLY REPORT **** ” echo ” ***CAMPAIGN 2000 CONTRIBUTIONS ***” echo “——————————————————————-” echo ” NAME PHONE JAN| Feb| Mar| Total Danated ” echo “——————————————————————-” awk -F: ‘{printf( “%-20s%12s%5d%5d%5d\t%5d\n”,$1,$2,$3,$4,$5,$3+$4+$5) } ‘ linux2.txt echo “——————————————————————-” echo ” SUMMARY ” echo “——————————————————————-” awk -F: ‘BEGIN{sum=0;total=0} {total=$3+$4+$5;sum+=total} END { printf(“The campaign received a total of $%d for this quartor\n”,sum) }’ linux2.txt awk -F: ‘BEGIN{average=0;i=0;total=0;sum=0} {total=$3+$4+$5;sum+=total;i++} END {average=sum/i;printf(“The average donation for the %d contributors was $%.2f.\n”,i,average) }’ linux2.txt awk -F: ‘BEGIN{highest=0;sum=0;total=0;name} { total=$3+$4+$5;if(total> highest) {highest=total;name=$1}} END { printf(“The highest total contribution was $%.2f made by %s\n”,highest,name) }’ linux2.txt echo ” ***THANKS Dan*** ” echo “The following people donated over \$500 to the campaign ” echo “They are eligible for the quarterly drawing!! ” echo “Listed are their names (sorted by last names) and phone numbers: ” awk -F: ‘BEGIN{ OFS=”–“;biaozhun=500;total=0} { $1=”\t”$1;total=$3+$4+$5;if(total>biaozhun) print $1,$2 | “sort -k 2″ }’ linux2.txt echo ” Thanks to all of you for your continued support!! “

运行效果

参考资料

Linux awk 内置函数详细介绍(实例)

如果说,罗马是一座厚重和凝固的堡垒,

AWK使用练习

相关文章:

你感兴趣的文章:

标签云: