LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Learning AWK language: (https://www.linuxquestions.org/questions/programming-9/learning-awk-language-670326/)

BadBoy1954 09-16-2008 02:32 PM

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

jan61 09-16-2008 02:48 PM

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.

BadBoy1954 09-16-2008 04:28 PM

Quote:

Originally Posted by jan61 (Post 3282326)
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

chrism01 09-16-2008 07:02 PM

Sometimes it helps to have alternative sources of info. Here's a good one: http://www.grymoire.com/Unix/Awk.html

jan61 09-17-2008 02:57 PM

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

BadBoy1954 09-18-2008 05:33 AM

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

jan61 09-18-2008 01:05 PM

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


All times are GMT -5. The time now is 02:32 AM.