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

  Go Back    


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


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-13-2013
radius radius is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 1 September 2014, 4:12 AM EDT
Posts: 80
Thanks: 25
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 ----------
Sponsored Links
    #2  
Old 11-13-2013
krishmaths krishmaths is offline
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)
Sponsored Links
    #3  
Old 11-13-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 2 September 2014, 5:20 PM EDT
Location: Aachen, Germany
Posts: 4,163
Thanks: 69
Thanked 995 Times in 943 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  
Old 11-13-2013
radius radius is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 1 September 2014, 4:12 AM EDT
Posts: 80
Thanks: 25
Thanked 0 Times in 0 Posts
big thanks..both code is correct..seems the last code is quicker
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Solved] Perform an operation to all directories Blue Solo Shell Programming and Scripting 0 11-02-2012 03:46 AM
[Solved] sum up third and second columns by 0 difference jacobs.smith Shell Programming and Scripting 4 09-16-2012 10:26 PM
[Solved] Remove Duplicates and Sum in a spreadsheet Giorgio C UNIX for Dummies Questions & Answers 2 05-30-2012 11:47 AM
[Solved] Mathematical operation in multiple files guns Shell Programming and Scripting 3 11-04-2011 01:08 PM
Problems with sum operation burakkilic UNIX for Dummies Questions & Answers 2 05-15-2006 12:52 PM



All times are GMT -4. The time now is 05:24 PM.