The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
summing up iostat values in AIX achak01 Shell Programming and Scripting 1 3 Weeks Ago 11:20 AM
summing values of a column mohsin.quazi Shell Programming and Scripting 3 08-24-2009 10:45 PM
Combine multiple string into 1 string group by certain criteria whchee Shell Programming and Scripting 4 05-11-2009 03:28 AM
Summing amounts mohan705 Shell Programming and Scripting 4 12-24-2008 07:13 AM
Summing on column sbasetty Shell Programming and Scripting 2 02-07-2007 08:15 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
gianluca2 gianluca2 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
Awk: Summing values with group criteria

Hi Guys,

I have a text file with ";" like separator

F1;F2;F3;F4;F5
444;100041;IT;GLOB;1800000000
444;100041;TM;GLOB;1000000000
444;10300264;IT;GLOB;2000000000
444;10300264;IT;GLOB;2500000000

I have to sum the cullums F5 for same F2 and F3 collums
The result must be:

444;100041;IT;GLOB;1800000000
444;100041;TM;GLOB;1000000000
444;10300264;IT;GLOB;4500000000

Thanks for your support.
Regards
Gianluca
  #2 (permalink)  
Old 2 Weeks Ago
ahmad.diab's Avatar
ahmad.diab ahmad.diab is offline
Registered User
  
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 228
code:-

Code:
nawk -F";"  -v OFS=";" '
NF{a[$2";"$3]+=$5 ; b[$2";"$3]=$1";"$4}
END{ for (i in a) {print i,b[i],a[i] } }
'  input_file | sort | nawk -F";" -v OFS=";" '{print $3,$1,$2,$4,$5}'
BR

---------- Post updated at 11:45 AM ---------- Previous update was at 11:33 AM ----------

or better one and more elegant:-
Code:
nawk  -F";"  -v OFS=";" '
NF{a[$2";"$3]+=$5 ; b[$2";"$3]=$1";"$2";"$3";"$4}
END{ for (i in a) {print b[i],a[i] } }
'  input_file | sort > out_file
BR
  #3 (permalink)  
Old 2 Weeks Ago
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Code:
awk 'BEGIN{FS=OFS=";"}NR>1{a[$2FS$3]+=$NF;$NF="";b[$2";"$3]=$0}END{for(i in a)print b[i]a[i]}' file
  #4 (permalink)  
Old 2 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 597
Code:
awk -F';' '{A[$2$3]=$1FS$2FS$3FS$4;B[$2$3]+=$5} END{for (i in A) printf "%s%.0f\n",A[i]FS,B[i]}' infile |sort -nt';'
Code:
444;100041;IT;GLOB;1800000000
444;100041;TM;GLOB;1000000000
444;10300264;IT;GLOB;4500000000
-or-
Code:
awk -F';' '{B[$2$3]+=$5;$5="";A[$2$3]=$0} END{for (i in A) printf "%s%.0f\n",A[i],B[i]}' infile |sort -n|tr ' ' ';'

Last edited by Scrutinizer; 2 Weeks Ago at 02:06 PM..
  #5 (permalink)  
Old 2 Weeks Ago
gianluca2 gianluca2 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
Work but...

Hi guys,

Many thanks for your answers & solution.
I have a little issue. If the source trace change as is

444;100041;IT;GLOB;1800000000;;;;;I
444;100041;TM;GLOB;1000000000;;;;;I
444;10300264;IT;GLOB;4500000000;;;;;I

the awk drop the chars after the number ";;;;;I"

What i have to change?
Thanks and regards
Gianluca
  #6 (permalink)  
Old 2 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 597
Try this:
Code:
awk 'BEGIN{OFS=FS=";"}{B[$2$3]+=$5;$5="@";A[$2$3]=$0} END{ for (i in A) {sub(/@/,sprintf("%.0f",B[i]),A[i]); print A[i]}}' infile | sort -nt';'
-or-
Code:
awk -F';' '{B[$2$3]+=$5;$5="@";A[$2$3]=$0} END{ for (i in A) {sub(/@/,sprintf("%.0f",B[i]),A[i]); print A[i]}}' infile |sort -n|tr ' ' ';'
  #7 (permalink)  
Old 2 Weeks Ago
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
1st Place Winner (TOTW)  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Award Winner

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
Code:
awk -F\; 'END { for (_ in _0) {
  l = split(_0[_], t); t[5] = _5[_]
  for (i=1; i<=l; i++) 
    printf "%s", t[i] (i == l ? RS : FS)  
    }      
  }  
{ _5[$2,$3] += $5; _0[$2,$3] = $0 }   
' infile


---------- Post updated at 01:03 PM ---------- Previous update was at 12:54 PM ----------

If the file is already ordered:
Code:
awk -F\; 'END { print r }
!_[$2,$3]++ && NR > 1 { 
  print r; x = 0 
  }
{ $5 = x += $5; r = $0 }
' OFS=\; infile
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0