Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-16-2013
radius radius is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 7 July 2015, 2:02 AM EDT
Posts: 120
Thanks: 47
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 Unix and Linux 08-16-2013
bartus11's Unix or Linux Image
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 June 2015, 6:17 PM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 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 Unix and Linux 08-16-2013
radius radius is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 7 July 2015, 2:02 AM EDT
Posts: 120
Thanks: 47
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 Unix and Linux 08-16-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 25 June 2015, 6:26 PM EDT
Location: Houston, Texas, USA
Posts: 682
Thanks: 0
Thanked 206 Times in 198 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 Unix and Linux 08-17-2013
radius radius is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 7 July 2015, 2:02 AM EDT
Posts: 120
Thanks: 47
Thanked 0 Times in 0 Posts
it wont work sir..any suggestion?
Sponsored Links
    #6  
Old Unix and Linux 08-17-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 7 July 2015, 2:28 AM EDT
Location: Amsterdam
Posts: 10,311
Thanks: 347
Thanked 2,725 Times in 2,437 Posts
What won't work? Please elaborate... What is your OS and version?
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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:04 AM.