Uw.shell by example, 3rd Edition chapter 7th answers to awk programming chapter

Source: Internet
Author: User

Run the command awk-F scriptname filename.

For example, if the first script name is bigawk. SC1, the file name is datafile # The content of datafile is the same as the previous content, so I will not post it here. You can check out the content in my previous section.
The actual command: awk-F bigawk. SC1 datafile

The last parameter is passed through the command line.

The actual command is: awk-F myfunc. SC 3 datafile

Here, you can use numbers 1, 2, and 3 to specify the average value of the contribution of everyone in the current month.

 

 

 

 

# Comment: this is awk script
# Name: bigawk. SC1
# Position: 7.5 Review
# Function: output table by use datafile

Begin {FS = ":"; _ 2 ndmax = 0; _ 1 stmin = 1000;
Printf "/T *** campaign 1998 contributions ***/T/N"
Printf "----------------------------------------------------------------------------/N"
Printf "name/T phone/T/tjan | Feb | Mar | Total donated/N"
Printf "----------------------------------------------------------------------------/N"
}
{Total = $3 + $4 + $5; sum + = total; printf "%-19 s %-19 s % 6.2f % 9.2f % 9.2f % 9.2f/N ", $1, $2, $3, $4, $5, total}
{_ 2 ndmax = (_ 2 ndmax> $4 )? _ 2 ndmax: $4}
{_ 1 stmin = (_ 1 stmin <$3 )? _ 1 stmin: $3}
End {
Printf "----------------------------------------------------------------------------/N"
Printf "/T Summary/N"
Printf "----------------------------------------------------------------------------/N"
Printf "The campaign received a total of $ % 5.2f for this quarter./N", Sum
Printf "The average donation for the % s contributors was $ % 5.2f./N", NR, sum/NR
Printf "the second month highest contribution was $ % 5.2f./N", _ 2 ndmax
Printf "the first month lowest contribution was $ % 5.2f./N", _ 1 stmin
}

 

 

# Comment: this is awk script
# Name: bigawk. SC2
# Position: 7.13 Review
# Function: output table by use datafile

Begin {FS = ":";
Printf "/T *** first quarterly report ***/N"
Printf "/T *** campaign 1998 contributions ***/T/N"
Printf "----------------------------------------------------------------------------/N"
Printf "name/T phone/T/tjan | Feb | Mar | Total donated/N"
Printf "----------------------------------------------------------------------------/N"
}
{Total = $3 + $4 + $5; sum + = total; printf "%-19 s %-19 s % 6.2f % 9.2f % 9.2f % 9.2f/N ", $1, $2, $3, $4, $5, total}

# Find first quarterly who was contribution total Max
{If (maxtotal <total ){
Maxtotal = Total
Thankname = $1
}
}
# Find those donated over 500 and storage in array
{
If (total> 500 ){
Nphrecord [I ++] = $1 "--" $2
}

}
End {
Printf "----------------------------------------------------------------------------/N"
Printf "/T Summary/N"
Printf "----------------------------------------------------------------------------/N"
Printf "The campaign received a total of $ % 5.2f for this quarter./N", Sum
Printf "The average donation for the % s contributors was $ % 5.2f./N", NR, sum/NR
Printf "the highest total contribution was $ % 5.2f made by % s/n", maxtotal ,/
Thankname

Printf "/T *** thanks"
Split (thankname, namearray ,"")
Printf "% s ***/N", namearray [1]

Printf "the following people donated over $500 to the campaign./N"
Printf "they are eligible for the quarterly drawing !! /N"
Printf "listed are their names (sorted by last names) and phone numbers:/N"

# Use special for print those people who over donated 500 info.
For (I in nphrecord ){
Printf "/T % s/n", nphrecord [I]
}

Printf "/T thanks to all of you for your continued support !! /N"
}

 

 

# Comment: this is awk script
# Position: 7.15 Review
# Name: myfunc. SC
# Function: Use argument determine month to count average of that month donated

Begin {FS = ":"; month = argv [1]; Delete argv [1]; sum = 0}
Function month_donate (m ){
Sum + = $ (m + 2)
}
{Month_donate (month )}
End {print "in" month "average donated was" sum/NR}

 

 

 

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.