LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 09-16-2008, 03:32 PM   #1
BadBoy1954
LQ Newbie
 
Registered: Oct 2007
Location: Quebec,Canada
Distribution: PCLinuxOS
Posts: 3

Rep: Reputation: 0
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
 
Old 09-16-2008, 03:48 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
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.
 
Old 09-16-2008, 05:28 PM   #3
BadBoy1954
LQ Newbie
 
Registered: Oct 2007
Location: Quebec,Canada
Distribution: PCLinuxOS
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jan61 View Post
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
 
Old 09-16-2008, 08:02 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Sometimes it helps to have alternative sources of info. Here's a good one: http://www.grymoire.com/Unix/Awk.html
 
Old 09-17-2008, 03:57 PM   #5
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
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 03:58 PM.
 
Old 09-18-2008, 06:33 AM   #6
BadBoy1954
LQ Newbie
 
Registered: Oct 2007
Location: Quebec,Canada
Distribution: PCLinuxOS
Posts: 3

Original Poster
Rep: Reputation: 0
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 03:29 PM.
 
Old 09-18-2008, 02:05 PM   #7
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning a Foreign Language Software JMJ_coder Linux - Software 4 08-28-2012 04:38 PM
LXer: Intro to Awk, the Great Language with a Strange Name LXer Syndicated Linux News 0 07-15-2008 05:40 AM
learning sed,awk,postional parameter and functions yawe_frek Programming 3 09-08-2007 09:49 PM
LXer: Mind-blasting Japanese language learning tools LXer Syndicated Linux News 0 07-14-2007 09:33 AM
learning a foreign language using linux fisher99 Slackware 2 05-20-2003 11:27 PM


All times are GMT -5. The time now is 09:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration