awk print total

Thread Solved

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

awk print total

 
0
  #1
Jan 26th, 2009
#5report
function report
{
clear
echo "Name, Position, Department, Salary"
echo "========================================"
awk 'BEGIN{FS=","; RS="\n"} {printf "%s, %s, %s, %s\n", $1, $2, $3, $4}' $dataFile
awk '{total += $4}END{print total}' $dataFile
}

i am trying to print the total sum of the salary or $4 field. However i cant figure out whats wrong, i run the script and i get the total as 0.

My dataFile looks like this

John, Manager, Finance, $2000
Jack, Accountant, Finance, $1500
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: awk print total

 
0
  #2
Jan 26th, 2009
I would have hoped after 40 posts that you would have figured this out.
Or at least read it.
http://www.daniweb.com/forums/announcement113-3.html
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: awk print total

 
0
  #3
Jan 26th, 2009
John, Manager, Finance, $2000
Jack, Accountant, Finance, $1500

If you take off the $ sign from the field value it might work.

[Edit] Don't disregard Salem's advice. You're lucky I didn't noticed it before. I usually don't entertain posts from people that willingly ignores the forums conduct.
Last edited by Aia; Jan 26th, 2009 at 3:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

Re: awk print total

 
0
  #4
Jan 26th, 2009
sorry bout that....my net got dc-ed halfway thru posting and i copied my whole post, didnt realise that code tags wont appear when copied...

anyway i tried taking off the $ sign but it still gives me the total as 0
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: awk print total

 
0
  #5
Jan 27th, 2009
Strange, that would have worked - post your latest code.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

Re: awk print total

 
0
  #6
Jan 27th, 2009
Shell Scripting Syntax (Toggle Plain Text)
  1. function report
  2. {
  3. clear
  4. echo "Name, Position, Department, Salary"
  5. echo "========================================"
  6. awk 'BEGIN{FS=","; RS="\n"} {printf "%s, %s, %s, %s\n", $1, $2, $3, $4}' $dataFile
  7. echo "Total:"
  8. awk '{total += $4}END{print total}' $dataFile
  9. }

this is my output after Aia's advice, it still doesnt work

John, Manager, Finance, 2000
Jack, Accountant, Finance, 1500
Total:
0
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: awk print total

 
0
  #7
Jan 27th, 2009
Hey There,

Two things, I think:

1. You're not stripping the literal dollar sign from the figures you're trying to add

2. Since you're invoking awk twice, you need to include your BEGIN def's twice

If you change the last line of your function to:

Shell Scripting Syntax (Toggle Plain Text)
  1. sed 's/\$//' $dataFile|awk 'BEGIN{FS=","; RS="\n"} {total += $4}END{print total}'

it should work (although it's a simplistic sed equation that assumes only one $ per line), but you could save yourself some headach by just combining the two awk statements.

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: awk print total

 
0
  #8
Jan 28th, 2009
The shell will allow you to create multi-line awk programs, which in this case would allow you to only read the file once, AND be sure that the $4 you see printed is the $4 being summed.
Shell Scripting Syntax (Toggle Plain Text)
  1. awk 'BEGIN {
  2. print "Name, Position, Department, Salary"
  3. print "========================================"
  4. FS=","
  5. RS="\n"
  6. }
  7. {printf "%s, %s, %s, %s\n", $1, $2, $3, $4}
  8. {total += $4}
  9. END {
  10. print total
  11. }' $dataFile
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC