[Solved] Sum operation | Unix Linux Forums | UNIX for Dummies Questions & Answers

 Member Name Remember Me? Password

 UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

# [Solved] Sum operation

## UNIX for Dummies Questions & Answers

#1
11-13-2013
 radius Registered User Join Date: Mar 2013 Last Activity: 8 December 2014, 6:55 AM EST Posts: 107 Thanks: 41 Thanked 0 Times in 0 Posts
[Solved] Sum operation

I have file input

Code:
```w34    AG1    2    184
w35    AG1    6    552
w35    BG1    12    0
w35    CD1    7    0
w36    CG1    4    0```

my output should be

Code:
```w34       AG1       2    184    0.991
w35       AG1       6    552    0.991
w35       BG1       12    0    1.000
w35       CD1       7    0    1.000
w36       CG1       4    0    1.000```

i did
Code:
`awk '{a[\$1]+=\$3;b[\$1]+=\$4} END {for (i in a) print i,a[i],b[i],(1-(b[i]/(7*24*60*a[i])))}' `

pls help me correct the code

---------- Post updated at 04:22 AM ---------- Previous update was at 04:20 AM ----------
#2
11-13-2013
 krishmaths Registered User Join Date: Sep 2006 Last Activity: 30 June 2014, 4:32 AM EDT Location: Chennai, India Posts: 443 Thanks: 22 Thanked 70 Times in 68 Posts
Change your awk to

Code:
`awk '{a[NR]=\$3;b[NR]=\$4;row[NR]=\$0} END {for (i in a) printf("%s\t%.3f\n", row[i],(1-(b[i]/(7*24*60*a[i]))))}' infile`

 The Following User Says Thank You to krishmaths For This Useful Post: radius (11-13-2013)
#3
11-13-2013
 RudiC   Registered User Join Date: Jul 2012 Last Activity: 21 December 2014, 4:45 AM EST Location: Aachen, Germany Posts: 4,888 Thanks: 80 Thanked 1,224 Times in 1,150 Posts
Why all those arrays? Shouldn't
Code:
`awk '{printf("%s\t%.3f\n", \$0,(1-(\$4/(7*24*60*\$3))))}' file`

be sufficient?
 The Following User Says Thank You to RudiC For This Useful Post: radius (11-13-2013)
#4
11-13-2013
 radius Registered User Join Date: Mar 2013 Last Activity: 8 December 2014, 6:55 AM EST Posts: 107 Thanks: 41 Thanked 0 Times in 0 Posts
big thanks..both code is correct..seems the last code is quicker