Dealing with sum | 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 !!

Dealing with sum

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-16-2013
radius radius is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 3 March 2014, 4:03 AM EST
Posts: 78
Thanks: 25
Thanked 0 Times in 0 Posts
Dealing with sum

I have file input

Code:
1/1/2013    1AS030A    0    1083    CHINA
1/1/2013    1AS030B    0    675        KOREA
1/1/2013    1AS035A    162    662        CHINA
1/1/2013    1AS035B    51    799        INDIA
1/1/2013    1AS035C    0    731        CHINA
1/2/2013    1AS073A    10    1375    KOREA
1/2/2013    1AS073B    0    89        KOREA
1/2/2013    1AS073C    0    159        INDIA
1/2/2013    1AS149A    3    987        CHINA
1/3/2013    1AS149B    97    1324    CHINA
1/3/2013    1AS149C    104    1030    CHINA
1/3/2013    1AS149E    0    826        CHINA
1/3/2013    1AS149F    0    1592    CHINA
1/4/2013    1AS149G    0    1682    KOREA
1/4/2013    1AS151A    0    886        KOREA
1/4/2013    1AS151B    0    969        INDIA
1/4/2013    1AS151C    5    1271    INDIA
1/4/2013    1AS152A    278    1560    KOREA
1/4/2013    1AS152B    0    871        INDIA
1/4/2013    1AS152C    0    887        CHINA

output I expect to have is


Code:
1/1/2013    CHINA    162    2476    6.54%
1/1/2013    INDIA    51    799        6.38%
1/1/2013    KOREA    0    675        0.00%
1/2/2013    CHINA    3    987        0.30%
1/2/2013    INDIA    0    159        0.00%
1/2/2013    KOREA    10    1464    0.68%
1/3/2013    CHINA    201    4772    4.21%
1/4/2013    CHINA    0    887        0.00%
1/4/2013    INDIA    5    3111    0.16%
1/4/2013    KOREA    278    4128    6.73%

sum all column3 when its column5(country) and column1(date) are same and also sum column4, print its percentage

thanks
Sponsored Links
    #2  
Old 08-16-2013
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 24 April 2014, 9:59 AM EDT
Posts: 3,685
Thanks: 6
Thanked 1,136 Times in 1,107 Posts
Try:
Code:
awk '{x=$1" "$5;a[x]+=$3;b[x]+=$4}END{for (i in a) print i,a[i],b[i],sprintf ("%.2f%", a[i]*100/b[i])}' input | sort

Sponsored Links
    #3  
Old 08-16-2013
radius radius is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 3 March 2014, 4:03 AM EST
Posts: 78
Thanks: 25
Thanked 0 Times in 0 Posts
thanks mr bartus11,

but when i run the script for such a big file (114000 rows), it won't work well


Code:
nawk: division by zero
 input record number 1.13053e+06, file dodol.dat
 source line number 1
sort: missing NEWLINE added at end of input file STDIN

and also only certain date printed

thanks
    #4  
Old 08-16-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 17 April 2014, 5:28 PM EDT
Location: Houston, Texas, USA
Posts: 660
Thanks: 0
Thanked 200 Times in 192 Posts
try also:

Code:
< infile dos2ux | awk '
function pr () {
   for (i in c) {
      if (b[i]) { p=(a[i]/b[i])*100 } else {p=0};
      printf ("%-10s   %8s %5d   %5d   %6.2f%%\n", d, i, a[i], b[i], p);
      delete c[i]; delete a[i]; delete b[i];
   }
}
! r[$1]++ { pr() }
{d=$1; c[$NF]=$NF; a[$NF]+=$3; b[$NF]+=$4;}
END { pr() }
' | sort

Sponsored Links
    #5  
Old 08-17-2013
radius radius is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 3 March 2014, 4:03 AM EST
Posts: 78
Thanks: 25
Thanked 0 Times in 0 Posts
it wont work sir..any suggestion?
Sponsored Links
    #6  
Old 08-17-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 24 April 2014, 7:24 PM EDT
Location: Amsterdam
Posts: 8,901
Thanks: 230
Thanked 2,147 Times in 1,928 Posts
What won't work? Please elaborate... What is your OS and version?
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
Dealing with geany core figaro UNIX and Linux Applications 2 04-09-2011 02:20 AM
Need help with Card Dealing Program Izzy123 Programming 3 03-28-2011 02:18 PM
Print sum and relative value of the sum saint2006 Shell Programming and Scripting 2 07-08-2010 10:37 AM
Dealing with files with spaces in the name jakethegreycat Shell Programming and Scripting 10 10-09-2009 09:28 PM
Help in dealing with arra jojan Shell Programming and Scripting 5 08-28-2007 02:14 PM



All times are GMT -4. The time now is 07:37 PM.