Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-16-2008, 02:32 PM
|
#1
|
|
LQ Newbie
Registered: Oct 2007
Location: Quebec,Canada
Distribution: PCLinuxOS
Posts: 3
Rep:
|
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
|
|
|
|
09-16-2008, 02:48 PM
|
#2
|
|
Member
Registered: Jun 2008
Posts: 235
Rep:
|
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.
|
|
|
|
09-16-2008, 04:28 PM
|
#3
|
|
LQ Newbie
Registered: Oct 2007
Location: Quebec,Canada
Distribution: PCLinuxOS
Posts: 3
Original Poster
Rep:
|
Quote:
Originally Posted by jan61
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.
|
I used to be a woodworker before this.(I know it not a excuse) everything
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
|
|
|
|
09-16-2008, 07:02 PM
|
#4
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
Sometimes it helps to have alternative sources of info. Here's a good one: http://www.grymoire.com/Unix/Awk.html
|
|
|
|
09-17-2008, 02:57 PM
|
#5
|
|
Member
Registered: Jun 2008
Posts: 235
Rep:
|
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
^^^ define the field delimiter
^^^^^^^^^^^^^^^ define an awk script to execute
^^^^ the input data file
In my opinion it makes it easier to sort the things out.
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
{ firstname = gensub(/ .*/, "", "g", $1); # extract last name from first field
lastname = gensub(/.* /, "", "g", $1); # extract first name from first field
phone = $2; # phone number is second field
total = $3 + $4 + $5; # get the total amount
if (total > 500) { # process only if total > 500
# the next line builds my sort criteria
sort_line = sprintf("%-30s %-20s %-40s", lastname, firstname, phone);
print sort_line; # this is to show you how the sort criteria looks like
# now insert the sort criteria into an array
top_sponsors_sort[sort_line] = sort_line;
# insert the requested output to another array
# we use the same index "sort_line" to be able to
# refer from top_sponsors_sort to this array
top_sponsors[sort_line] = firstname " " lastname phone;
}
}
END { # all data is read, do the sort and output
print "===> top sponsors <==="; # a header line
num_elements = asort(top_sponsors_sort); # sort the top_sponsors_sort array
# now output in the sorted order
for (cnt = 1; cnt <= num_elements; cnt++)
print top_sponsors[top_sponsors_sort[cnt]];
}
And now the output:
Code:
jan@jack:~/tmp/sort_sponsors> awk -F: -f sponsors.awk data
Harrington Mike 510 548-1278
McNichol Archie 206 548-1348
Quigley Guy 916 343-6410
Savage Dan 406 298-7744
Goldenrod John 916 348-4278
Savage Tom 408 926-3456
Stachelin Elizabeth 916 440-1763
===> top sponsors <===
John Goldenrod 916 348-4278
Mike Harrington 510 548-1278
Archie McNichol 206 548-1348
Guy Quigley 916 343-6410
Dan Savage 406 298-7744
Tom Savage 408 926-3456
Elizabeth Stachelin 916 440-1763
Hope that helps a little bit.
Jan
Last edited by jan61; 09-17-2008 at 02:58 PM.
|
|
|
|
09-18-2008, 05:33 AM
|
#6
|
|
LQ Newbie
Registered: Oct 2007
Location: Quebec,Canada
Distribution: PCLinuxOS
Posts: 3
Original Poster
Rep:
|
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
Last edited by BadBoy1954; 09-18-2008 at 02:29 PM.
|
|
|
|
09-18-2008, 01:05 PM
|
#7
|
|
Member
Registered: Jun 2008
Posts: 235
Rep:
|
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];}'
a ==> b
Jan
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:26 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|