Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Dealing with sum

UNIX for Dummies Questions & Answers


 
 
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: 4 December 2016, 9:14 PM EST
Posts: 134
Thanks: 50
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
Registered User
 
Join Date: Apr 2009
Last Activity: 3 August 2016, 11:03 AM 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: 4 December 2016, 9:14 PM EST
Posts: 134
Thanks: 50
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: 7 December 2016, 9:36 PM EST
Location: Houston, Texas, USA
Posts: 873
Thanks: 0
Thanked 299 Times in 286 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: 4 December 2016, 9:14 PM EST
Posts: 134
Thanks: 50
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 online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 9 December 2016, 10:21 AM EST
Location: Amsterdam
Posts: 11,107
Thanks: 440
Thanked 3,111 Times in 2,751 Posts
What won't work? Please elaborate... What is your OS and version?
Sponsored Links
 

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 03:20 AM
Need help with Card Dealing Program Izzy123 Programming 3 03-28-2011 03:18 PM
Print sum and relative value of the sum saint2006 Shell Programming and Scripting 2 07-08-2010 11:37 AM
Dealing with files with spaces in the name jakethegreycat Shell Programming and Scripting 10 10-09-2009 10:28 PM
Help in dealing with arra jojan Shell Programming and Scripting 5 08-28-2007 03:14 PM



All times are GMT -4. The time now is 11:24 AM.