LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2011, 01:52 PM   #1
JoeRogan
LQ Newbie
 
Registered: Nov 2011
Posts: 20

Rep: Reputation: Disabled
Need help with newbie scripting!!!


Ok so i need this out come


***CAMPAIGN 1998 CONTRIBUTIONS***

---------------------------------------------------------------------------

NAME PHONE Jan | Feb | Mar | Total Donated

---------------------------------------------------------------------------

Mike Harrington (510) 548-1278 250.00 100.00 175.00 525.00

Christian Dobbins (408) 538-2358 155.00 90.00 201.00 446.00

Susan Dalsass (206) 654-6279 250.00 60.00 50.00 360.00

Archie McNichol (206) 548-1348 250.00 100.00 175.00 525.00

Jody Savage (206) 548-1278 15.00 188.00 150.00 353.00

Guy Quigley (916) 343-6410 250.00 100.00 175.00 525.00

Dan Savage (406) 298-7744 450.00 300.00 275.00 1025.00

Nancy McNeil (206) 548-1278 250.00 80.00 75.00 405.00

John Goldenrod (916) 348-4278 250.00 100.00 175.00 525.00

Chet Main (510) 548-5258 50.00 95.00 135.00 280.00

Tom Savage (408) 926-3456 250.00 68.00 200.00 618.00

Elizabeth Stachelin (916) 440-1763 175.00 75.00 300.00 550.00

----------------------------------------------------------------------------

SUMMARY

----------------------------------------------------------------------------

The campaign received a total of $6137.00 for this quarter.

The average donation for the 12 contributors was $511.42.

The highest contribution was $300.00.

The lowest contribution was $15.00.


with this data




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


And this is my script....what am i doing wrong?

BEGIN{FS=":"
print "\t\t\t***CAMPAIGN 1998 CONTRIBUTIONS***"
print "---------------------------------------------------------"
print "NAME\t\t\tPHONE\t\t\tPHONE\t\t\tJan | Feb | Mar | Total Donation"
print "---------------------------------------------------------\n"
}
{total+=$3$4$5}
{average=total/12}
{print $1\t\t$2\t\t\t$3, $4, $5, total}
END{print "------------------------------------------------------------\n"
print "\t\t\t\tSUMMARY"
print "---------------------------------------------------------\n"
}
{print "The campaign received a total of $" total " for this quarter."}
{print "The average donation for the 12 contributors was" average "."}


something is wrong with this message box thingy but the outcome is suppose to be in order. The data is suppose to be under the proper categories

Last edited by JoeRogan; 11-25-2011 at 02:00 PM. Reason: not what i ment to type
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-25-2011, 04:04 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
How about you tell us what you see, explain how it doesn't match what you
expect, and then we'll discuss where you went wrong ...

To get the thought process started: the first thing I see is that the TABs
on line 9 aren't in quotes, so you're going to have a syntax error there
to begin with.


Cheers,
Tink
 
Old 11-25-2011, 04:49 PM   #3
JoeRogan
LQ Newbie
 
Registered: Nov 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
This is what i get with the script i typed in...

***CAMPAIGN 1998 CONTRIBUTIONS***
---------------------------------------------------------
NAME PHONE PHONE Jan | Feb | Mar | Total Donation
---------------------------------------------------------

Mike Harrington (510) 548-1278 250 100 175 250100175
The campaign received a total of $250100175 for this quarter.
The average donation for the 12 contributors was2.08417e+07.
250100175
The campaign received a total of $250100175 for this quarter.
The average donation for the 12 contributors was2.08417e+07.
Christian Dobbins (408) 538-2358 155 90 201 265690376
The campaign received a total of $265690376 for this quarter.
The average donation for the 12 contributors was2.21409e+07.
265690376
The campaign received a total of $265690376 for this quarter.
The average donation for the 12 contributors was2.21409e+07.
Susan Dalsass (206) 654-6279 250 60 50 268196426
The campaign received a total of $268196426 for this quarter.
The average donation for the 12 contributors was2.23497e+07.
268196426
The campaign received a total of $268196426 for this quarter.
The average donation for the 12 contributors was2.23497e+07.
Archie McNichol (206) 548-1348 250 100 175 518296601
The campaign received a total of $518296601 for this quarter.
The average donation for the 12 contributors was4.31914e+07.
518296601
The campaign received a total of $518296601 for this quarter.
The average donation for the 12 contributors was4.31914e+07.
Jody Savage (206) 548-1278 15 188 150 533484751
The campaign received a total of $533484751 for this quarter.
The average donation for the 12 contributors was4.44571e+07.
533484751
The campaign received a total of $533484751 for this quarter.
The average donation for the 12 contributors was4.44571e+07.
Guy Quigley (916) 343-6410 250 100 175 783584926
The campaign received a total of $783584926 for this quarter.
The average donation for the 12 contributors was6.52987e+07.
783584926
The campaign received a total of $783584926 for this quarter.
The average donation for the 12 contributors was6.52987e+07.
Dan Savage (406) 298-7744 450 300 275 1233885201
The campaign received a total of $1233885201 for this quarter.
The average donation for the 12 contributors was1.02824e+08.
1233885201
The campaign received a total of $1233885201 for this quarter.
The average donation for the 12 contributors was1.02824e+08.
Nancy McNeil (206) 548-1278 250 80 75 1236393276
The campaign received a total of $1236393276 for this quarter.
The average donation for the 12 contributors was103032773.
1236393276
The campaign received a total of $1236393276 for this quarter.
The average donation for the 12 contributors was103032773.
John Goldenrod (916) 348-4278 250 100 175 1486493451
The campaign received a total of $1486493451 for this quarter.
The average donation for the 12 contributors was1.23874e+08.
1486493451
The campaign received a total of $1486493451 for this quarter.
The average donation for the 12 contributors was1.23874e+08.
Chet Main (510) 548-5258 50 95 135 1491588586
The campaign received a total of $1491588586 for this quarter.
The average donation for the 12 contributors was1.24299e+08.
1491588586
The campaign received a total of $1491588586 for this quarter.
The average donation for the 12 contributors was1.24299e+08.
Tom Savage (408) 926-3456 250 168 200 1741756786
The campaign received a total of $1741756786 for this quarter.
The average donation for the 12 contributors was1.45146e+08.
1741756786
The campaign received a total of $1741756786 for this quarter.
The average donation for the 12 contributors was1.45146e+08.
Elizabeth Stachelin (916) 440-1763 175 75 300 1759332086
The campaign received a total of $1759332086 for this quarter.
The average donation for the 12 contributors was1.46611e+08.
------------------------------------------------------------

SUMMARY
---------------------------------------------------------


what i need is something like this


***CAMPAIGN 1998 CONTRIBUTIONS***

---------------------------------------------------------------------------

NAME PHONE Jan | Feb | Mar | Total Donated

---------------------------------------------------------------------------

Mike Harrington (510) 548-1278 250.00 100.00 175.00 525.00

Christian Dobbins (408) 538-2358 155.00 90.00 201.00 446.00

Susan Dalsass (206) 654-6279 250.00 60.00 50.00 360.00

Archie McNichol (206) 548-1348 250.00 100.00 175.00 525.00

Jody Savage (206) 548-1278 15.00 188.00 150.00 353.00

Guy Quigley (916) 343-6410 250.00 100.00 175.00 525.00

Dan Savage (406) 298-7744 450.00 300.00 275.00 1025.00

Nancy McNeil (206) 548-1278 250.00 80.00 75.00 405.00

John Goldenrod (916) 348-4278 250.00 100.00 175.00 525.00

Chet Main (510) 548-5258 50.00 95.00 135.00 280.00

Tom Savage (408) 926-3456 250.00 68.00 200.00 618.00

Elizabeth Stachelin (916) 440-1763 175.00 75.00 300.00 550.00

----------------------------------------------------------------------------

SUMMARY

----------------------------------------------------------------------------

The campaign received a total of $6137.00 for this quarter.

The average donation for the 12 contributors was $511.42.

The highest contribution was $300.00.

The lowest contribution was $15.00.
 
Old 11-25-2011, 06:23 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
You should use code tags to post that. It will put it in a scroll box with a monospaced font.
 
Old 11-25-2011, 08:25 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Ok ... here is what I see as problems:

1. Each opening brace is interpreted prior to END, hence out of order printing of final statements

2. += construct should be treated as a normal math problem, ie what you have written is:
Code:
total = total + $3$4$5
As you can see this is not what you want

3. As average only needs to be calculated at the end it should be put in END section (not wrong just not needed previously)

4. I am curious that you receive no errors because as Tinkster points out, the lack of quotes around the tab entries errors for me

I think if you fix the above items you might get closer to what you want.
 
Old 11-25-2011, 10:19 PM   #6
JoeRogan
LQ Newbie
 
Registered: Nov 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
I actually fixed what tinker pointed out..i will try to fix what u said..thanks will get back to u
 
Old 11-28-2011, 01:55 PM   #7
JoeRogan
LQ Newbie
 
Registered: Nov 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
BTW way guys im using Unix Shell By Examples Fourth Edition By ELLIE QUIGLEY to help me started with scripting and this book is confusing and too advanced for me..what do u guys suggest for a beginner? almost 60-70 % of the time im googling things to understand what they are saying..I need something VERY basic. PLZ and thx
 
Old 11-28-2011, 02:09 PM   #8
JoeRogan
LQ Newbie
 
Registered: Nov 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
Grail i fixed that issue. the problem now im having is how do i write a script for getting the total amount from the "Total Donated" section..i want to add all the data from that section and write a sentence that says.."The campaign received a total of $_____ for this quarter."
 
Old 11-28-2011, 02:10 PM   #9
JoeRogan
LQ Newbie
 
Registered: Nov 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
BTW this is not "homework" im doing this all on my own on my free time.
 
Old 11-28-2011, 02:37 PM   #10
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
You are using
Code:
total+=$3$4$5
to get the running total but this is concatanating the values not adding them together

I would have a different variable for the line total:
Code:
line_total = $3 + $4 + $5
total+=line_total
 
Old 11-28-2011, 02:47 PM   #11
JoeRogan
LQ Newbie
 
Registered: Nov 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
sick will try that
 
Old 11-28-2011, 03:01 PM   #12
JoeRogan
LQ Newbie
 
Registered: Nov 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
Ok so this is what i got so far...

oden$ awk -f dad gangster
***CAMPAIGN 1998 CONTRIBUTIONS***
---------------------------------------------------------
NAME PHONE Jan | Feb | Mar | Total Donation
---------------------------------------------------------

Mike Harrington (510) 548-1278 250 100 175 525
0
Christian Dobbins (408) 538-2358 155 90 201 446
0
Susan Dalsass (206) 654-6279 250 60 50 360
0
Archie McNichol (206) 548-1348 250 100 175 525
0
Jody Savage (206) 548-1278 15 188 150 353
0
Guy Quigley (916) 343-6410 250 100 175 525
0
Dan Savage (406) 298-7744 450 300 275 1025
0
Nancy McNeil (206) 548-1278 250 80 75 405
0
John Goldenrod (916) 348-4278 250 100 175 525
0
Chet Main (510) 548-5258 50 95 135 280
0
Tom Savage (408) 926-3456 250 168 200 618
0
Elizabeth Stachelin (916) 440-1763 175 75 300 550
------------------------------------------------------------

SUMMARY
---------------------------------------------------------

The campaign received a total of $6137 for this quarter.
The average donation for the 12 contributors was511.417.


I am getting very close.. i am using this script


print "\t\t\t***CAMPAIGN 1998 CONTRIBUTIONS***"
print "---------------------------------------------------------"
print "NAME\t\t\tPHONE\t\t\tJan | Feb | Mar | Total Donation"
print "---------------------------------------------------------\n"
}
{line_total=$3+$4+$5}
{total+=line_total}
{average=total/12}
{print $1"\t\t"$2"\t\t\t"$3, $4, $5, line_total}
END{print "------------------------------------------------------------\n"
print "\t\t\t\tSUMMARY"
print "---------------------------------------------------------\n"
print "The campaign received a total of $" total " for this quarter."
print "The average donation for the 12 contributors was" average "."}



Only a few issues..the datas are not alligning properly and how do i write a script where i divide the total quarterly by 12.
 
Old 11-28-2011, 07:03 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well first off this is an awk script so I would recommend http://www.gnu.org/software/gawk/man...ode/index.html as a learning tool.

Have a look at the formatting options for printf to help with alignment.
Quote:
how do i write a script where i divide the total quarterly by 12
What does this mean? You already have a divide by 12. How are you not able to implement a new calculation?
 
Old 11-29-2011, 06:44 AM   #14
JoeRogan
LQ Newbie
 
Registered: Nov 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
is it the right way to calculate the average?
 
Old 11-29-2011, 04:10 PM   #15
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
The only issue with calculating the average that I can see is that you are "hard coding" the number of donations to 12. It would be better to count the number of donations from the input file rather than specifying it.

I also have a slight issue with the input file, I would suggest removing the blank lines (although this could be coded for)

Have you looked at the reference provided by grail? This covers a lot more than print formating, check out regexp for removing blank lines from your input. and check out "Increment Ops" for a simple counter.
 
  


Reply



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
Newbie Needs Help On Scripting 104625 Linux - Software 5 11-27-2011 06:28 PM
Shell Scripting help---Newbie djfog Programming 8 07-01-2008 05:54 AM
Shell scripting help -- newbie rnj Linux - Newbie 1 09-12-2005 12:08 AM
Newbie Scripting, please help granny Programming 6 04-24-2003 11:46 PM
A Newbie,s Scripting Question newpenguin Linux - Newbie 7 10-07-2002 12:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:09 AM.

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
Open Source Consulting | Domain Registration