| | |
awk print total
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
#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
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
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
Or at least read it.
http://www.daniweb.com/forums/announcement113-3.html
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.
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.
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
Shell Scripting Syntax (Toggle Plain Text)
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 echo "Total:" awk '{total += $4}END{print total}' $dataFile }
this is my output after Aia's advice, it still doesnt work
John, Manager, Finance, 2000
Jack, Accountant, Finance, 1500
Total:
0
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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:
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
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)
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
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)
awk 'BEGIN { print "Name, Position, Department, Salary" print "========================================" FS="," RS="\n" } {printf "%s, %s, %s, %s\n", $1, $2, $3, $4} {total += $4} END { print total }' $dataFile
![]() |
Similar Threads
- BASH script - sales per each associate (3 files) (Shell Scripting)
- Find the total lines in the file (Shell Scripting)
- How to calculate total free memory (C)
Other Threads in the Shell Scripting Forum
- Previous Thread: noob question
- Next Thread: load-intensive shell script
| Thread Tools | Search this Thread |






