![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 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.. |
|
||||
|
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 |
|
||||
|
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';'
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 ' ' ';'
|
|
|||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|