The practice of using awk under Linux

Source: Internet
Author: User
Tags printf

Environment

RedHat Linux 9 + vwware 8.0 + SSH 3.2.9

Task

The following text contains the name, telephone number and amount of donations for partisan movements.

Name: Tel: January donation Status: February contribution situation: March donation situation

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

Nancymcneil: (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

Use any linux command script you can use to generate a report like this, note that the summery in the report contains some statistics about the donation.

Solve

 #!/bin/bash #filename: test.sh sourcename=$1 echo "$sourcename" Sed-i "s/:/:/g" SourceName  echo "***first Quarterly The" echo "***campaign contributions * * *" echo "-------------------------------------------------------------------" echo "NAME PHONE JA n| 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.tx T AWK-f: ' begin{average=0;i=0;total=0;sum=0} {total=$3+$4+$5;sum+=total;i++} end {average=sum/i;printf ("the Averag E donation for the%d contributors is $%.2f.\n ", i,average)} ' linux2.txt awk-f: ' Begin{highest=0;sum=0;total=0;na Me} {total=$3+$4+$5;if (total> highest) {highest=total;name=$1}} end {printf ("the highest total Co                               

Ntribution is $%.2f made by% s\n ", highest,name)} ' linux2.txt echo ' ***thanks "Echo" The following people donated over \$500 to the campaign "EC     
Ho "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;tot Al=0} {$1= "\ T" $1;total=$3+$4+$5;if (Total>biaozhun) Print $1,$2 | "Sort-k 2"} ' linux2.txt echo ' to all of your for your continued support!! ' 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.