Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
 
Reply to this topicStart new topic
> AWK
Larry
post Mar 29 2008, 05:35 PM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 1
Joined: 29-March 08
Member No.: 13,406



I'm trying to add a column of numbers which contain commas in a file called countries.txt; i.e, 123,456. The numbers are in the 2nd column of the file. The general structure of the command is:

awk '(tot+=$2); END {print "Total = " tot} countries.txt

When I execute this all I get is the sum of the numbers prior to the first comma. I know that I have to either parse the numbers or somehow get rid of the commas.

Can anyone tell me how to do this. Thanks.

Larry
Go to the top of the page
 
+Quote Post
markjr
post Mar 30 2008, 09:08 AM
Post #2


./configure
***

Group: Admin
Posts: 62
Joined: 9-February 06
Member No.: 6,054



You could rinse out the commas before it ever hits awk:

sed 's/,//g' | awk .....

Or maybe specify the input field separator in the BEGIN section of the awk script itself, which I think is

BEGIN { IFS=" " }

(or whatever non-comma field separates your columns)

-mark


QUOTE (Larry @ Mar 29 2008, 05:35 PM) *
I'm trying to add a column of numbers which contain commas in a file called countries.txt; i.e, 123,456. The numbers are in the 2nd column of the file. The general structure of the command is:

awk '(tot+=$2); END {print "Total = " tot} countries.txt

When I execute this all I get is the sum of the numbers prior to the first comma. I know that I have to either parse the numbers or somehow get rid of the commas.

Can anyone tell me how to do this. Thanks.

Larry
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 18th December 2017 - 11:46 AM