Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add data with commas 1

Status
Not open for further replies.

jestrada101

Technical User
Mar 28, 2003
332
How can I add data using awk or sed in a file that has commas?

example data

john 111,000
earl 112,000

I want to sum this data. I've tried the following but it will not add data with commas:

awk '{ sum += $4 }
END { print sum }' mydatafile.txt
 
what's the desired output of this sample file?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
To add field 2 containing comma instead of decimal point :

awk '{ sub(&quot;,&quot;, &quot;.&quot;, $2) ; sum+=$2} END {print sum}' mydatafile.txt


Jean Pierre.
 
Hi:

you could delete the commas first:

tr -d , < data.file

Ed
 
Thanks all... I ended up using sed to remove..

sed s/,/''/g data.file | awk '{ sum += $2 } END { print sum}'

JE
 
You can remove the comma inside your awk script :

awk '{gsub(&quot;,&quot;, &quot;&quot;, $2); sum+=$2} END {print sum}'

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top