problem while adding column values in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem while adding column values in awk
# 1  
Old 07-27-2009
problem while adding column values in awk

Hi,

I have a file "input.txt" with the following content :

5312,0,,,1,8,141.2,20090727
3714,0,,,1,8,285.87,20090727
5426,0,,,1,8,3.9,20090727
3871,0,,,1,8,30.4,20090727
9071,0,,,1,8,146.2,20090727
5141,0,,,1,8,2.8,20090727
0460,0,,,1,8,-0.1,20090727
7918,0,,,1,8,-0.1,20090727
1887,0,,,1,8,2,20090727
2083,0,,,1,8,22.8,20090727
6766,0,,,1,8,89.6,20090727
6001,0,,,1,8,2.3,20090727
9152,0,,,1,8,84.7,20090727
8138,0,,,1,8,31.9,20090727
1571,0,,,1,8,29.4,20090727
9730,0,,,1,8,408.7,20090727
8751,0,,,1,8,22.2,20090727
9015,0,,,1,8,983.7,20090727
6358,0,,,1,8,6.3,20090727

The 7th field contains both +ve and -ve values. I want to get the resultant sum all the values in the 7th field. I used the following awk command :

awk -F"," '{print $7}' input.txt | awk '{sum+=$1} END {print sum}'

I am getting this result : 5.29935e+06

How can i convert the result to a proper decimal value because i need the resultant value to be passed into another script as input.

---------- Post updated at 11:07 AM ---------- Previous update was at 11:05 AM ----------

Forgot to mention that the file "input.txt" contains about 40000 rows and the 7th field might have small values like 0.02 or 0.001 etc.
# 2  
Old 07-27-2009
Hi.

Multiply the result by 1.

i.e
Code:
END {print sum*1}

Or use printf: i.e.
Code:
END {printf( "%f\n", sum)}

Cheers,

Last edited by Scott; 07-27-2009 at 08:17 AM..
# 3  
Old 07-27-2009
One awk command is sufficient:

Code:
awk -F, '{s+=$7}END{print s}' file

# 4  
Old 07-27-2009
Bug

Thanks Scottn and Franklin,

That printf command worked and now i am getting the proper value :

# awk -F, '{s+=$7}END{printf( "%f\n", s)}' input.txt

5299354.810000


Thanks for the help guys !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Adding values of a column based on another column

Hello, I have a data such as this: ENSGALG00000000189 329 G A 4 2 0 ENSGALG00000000189 518 T C 5 1 0 ENSGALG00000000189 1104 G A 5 1 0 ENSGALG00000000187 3687 G T 5 1 0 ENSGALG00000000187 4533 A T 4 2 0 ENSGALG00000000233 5811 T C 4 2 0 ENSGALG00000000233 5998 C A 5 1 0 I want to... (3 Replies)
Discussion started by: Homa
3 Replies

3. Shell Programming and Scripting

Adding of two column values

Hi cat /tmp/xx.txt 1 4 1 5 1 6 2 1 2 1 2 1 i want to add the values of 2nd column resepect to 1st column values..for 1 in 1st column i need sum of all the values in 2nd column ..pls tell me hw to do it?? (8 Replies)
Discussion started by: Aditya.Gurgaon
8 Replies

4. Shell Programming and Scripting

Adding Column Values Using Pattern Match

Hi All, I have a file with data as below: A,FILE1_MYFILE_20130309_1038,80,25.60 B,FILE1_MYFILE_20130309_1038,24290,18543.38 C,FILE1_dsc_dlk_MYFILE_20130309_1038,3,10.10 A,FILE2_MYFILE_20130310_1039,85,110.10 B,FILE2_MYFILE_20130310_1039,10,12.10... (10 Replies)
Discussion started by: angshuman
10 Replies

5. Shell Programming and Scripting

Adding column values in a file

Hi, I am having a file in the following format. for aaaa 1111 1234 2222 3434 for bbbb 1111 3434.343 2222 2343 for cccc 3333 2343.343 4444 89000 for dddd 1111 5678.343 2222 890.3 aaaa 2343.343 bbbb 34343.343 (5 Replies)
Discussion started by: jpkumar10
5 Replies

6. UNIX for Dummies Questions & Answers

Adding column with values

Dear all, I need your help for my question please I have without header (space separated) and need to add two colomns at the beginning with values my file look like : rs1 a t 0.6 rs2 a c 0.3 rs3 t g 0.8 I need to a new file like: 1 100 rs1 a t 0.6 1 100 rs2 a c 0.3 1 100 rs3 t g... (3 Replies)
Discussion started by: biopsy
3 Replies

7. Shell Programming and Scripting

Adding successive values in awk

Hi, I have a large log file in the following format. Epoch-Time Bytes 899726401 20 899726401 30 899726402 40 899726402 10 899726402 50 899726403 50 899726403 ... (8 Replies)
Discussion started by: sajal.bhatia
8 Replies

8. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

9. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

10. Shell Programming and Scripting

Help in adding positive & negative values in a column

Hi Gurus, In my file I have an amount field from position 74 to 87, which contains values starting with '+' as well as '-'. I want to add all positive values in a varible called "CREDIT" and all negative values in a variable "DEBIT". I know, we can use grep to identify values with positive and... (4 Replies)
Discussion started by: berlin_germany
4 Replies
Login or Register to Ask a Question