Learning AWK language:
Hi
This is my first post and i hope i am in the right one for this type of question. I am in leaning AWK and i have reach a glitch in my script. The problem is that i have to print the ones that gave more than $500.00 in the 3 month period and sort there names in alphabetical order: last name first and phone number.(example) ( Dalsass Susan--(206) 654-6279 ) I have read the book up to this point so many time my eyes hurts. I just cant figure how to write the code to make it work. I have to use AWK only for this exercise. This is the script that i have so far. # Campaign funds for first quarter. # Need total funds. # The highest contributor. # Who gave more than 500.00. # There will be a drawing. # Have to sort there name and phone number in alphabetical order and last #name first name second and phone number. BEGIN{FS=":";OFMT="%.2f" print"\n" printf"%56s\n","****FIRST QUARTERLY REPORT****" printf"%62s\n","|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|" printf"%59s\n","***CAMPAIGN 2004 CONTRIBUTIONS***" print"--------------------------------------------------------\ ----------------------------------------" printf"%-26s","NAME" printf"%-16s","PHONE" printf"%-8s","JAN" printf"|%8s","FEB" printf"%5s","|" printf"%8s","MAR" printf"%5s","|" printf"%18s\n","TOTAL DONATION" print"---------------------------------------------------------\ ---------------------------------------" } { ($6 = $3 + $4 + $5) printf"%-20s", $1 printf"%-19s", $2 printf"$%7.2f", $3 printf"%-6s", $7 printf"$%7.2f", $4 printf"%-6s", $8 printf"$%7.2f", $5 printf"%-6s", $9 printf"$%7.2f\n", $6 } {average+=$6 / 12} {total +=$6} ($6 >= 1000){highest=$6} ($6 >= 1000){name=$1} END{ print"-------------------------------------------------------\ ----------------------------------------" printf"%52s\n", "%%%%% SUMMARY %%%%%" print"-------------------------------------------------------\ ----------------------------------------" print"The campaign received a total of ""$"total ".00" " for this quarter." printf"The average donation for the 12 contributors was ""$%6.2f\n", average print"The highest total contribution was ""$" highest".00"" made by "name"." {split("Dan Savage",d," ")} printf"%40s\n", "****THANKS "d[1] "****" print"The following people donated over $500.00 to the campaign" print"They are eligible for the quarterly drawing!!" print"Listed are their names (sorted by last names) and phone numbers:" printf"%6s\n","Thanks to all of you for your continued support!!" } This is the file i have to work with. Had to remove ( ) in front of area code of phone numbers it was interfering with the post. 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 THANKS |
Moin,
because you want to learn I do not post a solution. Just some hints: 1. Sorting a list of entries makes sense only in the END section - it's the only place where all data is read and available. Use the asort functions available in awk. 2. While reading the input lines try to store the top buyers (or sponsors) in an array, awk provides methods to fill and process arrays. 3. Store the data you want to display in a format that allows you to sort the array members using one single sort (e. g. with a constant width for each field). hth Jan EDIT: Please use the code tag for scripts and data examples. |
Quote:
that i know about computers i learn by my self no tutor. Thanks for the help but i don't know how to say this but you got me more confuse. How can i explain this the book i am using has some exercise to accomplish before you move on. At the moment there is just a brief mention of sort no asort but function sort to be exact. Read your info went back to the book to read on it i just cant figure it out???. I will read some more and try a few on the info that i have. Again THANKS |
Sometimes it helps to have alternative sources of info. Here's a good one: http://www.grymoire.com/Unix/Awk.html
|
Moin,
I didn't want to confuse you ;-) So let's look at a possible solution. I assume that every line of the input file looks like this: firstname lastname: phone:jan:feb:mar Am I right? In my sample I do not do a formatted output, I only want to show you, how arrays and sorting could be used. I call awk using a specific syntax: Code:
awk -F: -f sponsors.awk data Now the awk script (I inserted some comments to help you to understand what I do): Code:
jan@jack:~/tmp/sort_sponsors> cat sponsors.awk Code:
jan@jack:~/tmp/sort_sponsors> awk -F: -f sponsors.awk data Jan |
Tanks Everyone Especially jan61
Well jan61 that is exactly the info that i need especially the last section. How can i put this the gensub example is not part of the exercise that i am in at the moment.(I think 2 exercise further.) But it ok i am getting the grasp on the matter, my feeling is that i have to use the split() function to accomplish this one. I really do want to learn this so there no use of jumping two chapter to accomplish this. If its in this exercise that mean the info is in this section, i just have to stick with it. I am doing this in my spare time so as soon as i figure this i will let you know. AGAIN A BIG THANKS FOR YOUR HELP |
Moin,
yes, split() is another possibility to do the job: Code:
jan@jack:~/tmp/sort_sponsors> echo "a b:x" | awk -F: '{n=split($1,arr,/ /);print arr[1],"==>",arr[2];}' |
All times are GMT -5. The time now is 02:32 AM. |