Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grouping and Subtotals on Group

Status
Not open for further replies.

jriordan

MIS
Sep 9, 2002
14
US
Hi,
I am having a problem with getting subtotals after I have sorted and grouped data based on the first field.
Here is a small example of my input data:

/u01:ALPS:200000:100000:100000
/u02:UNDO:500000:200000:300000
/u02:SYSTEM:100000:100000:0
/u01:INDEX:300000:100000:200000
/usr:TRAIN:100000:50000:50000

I can get my output formated correctly with the exception of subtotals. For example this is a small example of what I been able to do so far.

FS Allocated Used Free
/u01 200000 100000 100000
300000 100000 200000

/u02 500000 200000 300000
100000 100000 0

/usr 100000 50000 50000

I am trying to subtotal (/u01, then u02, and /usr) in the
blank space under each group. The following is the sort script that I have been able to run with success.

BEGIN { FS = ":"}
{ if ($1 != prev) {
print ""
prev = $1
} else
$1 = ""
printf("%-10s %7d %7d %7d\n",
$1, $3, $4, $5)}

Thanks in advance
John


 
working on the 1. file exemple:
BEGIN { IFS = ":" }
/^\//{ allo += $3; rese += $4; used += $5 }
END{ print allo, rese, used }
------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
nawk -f subTotal.awk subTotal.txt

#------------------------- subTotal.awk --------------------
BEGIN {
FS=":"
}

{
cell=$3 FS $4 FS $5
if (!arr[$1])
arr[$1]=cell;
else
arr[$1]=arr[$1] SUBSEP cell;
}

END {
for (i in arr) {
printf("%s", i);
numCellARR=split(arr, cellARR, SUBSEP);
for(j=1; j <= numCellARR; j++) {

split(cellARR[j], tmpCell, FS);
printf(&quot;\t%s\t%s\t%s\n&quot;, tmpCell[1], tmpCell[2], tmpCell[3]);
}
}
}
#---------------------------- vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
ooops - that's better

#---------------- subTotal.awk
BEGIN {
FS=&quot;:&quot;
}

{
cell=$3 FS $4 FS $5
if (!arr[$1])
arr[$1]=cell;
else
arr[$1]=arr[$1] SUBSEP cell;
}

END {
for (i in arr) {
printf(&quot;%s&quot;, i);
numCellARR=split(arr, cellARR, SUBSEP);
allo=0; res=0; used=0;
for(j=1; j <= numCellARR; j++) {
split(cellARR[j], tmpCell, FS);
printf(&quot;\t%s\t%s\t%s\n&quot;, tmpCell[1], tmpCell[2], tmpCell[3]);
allo+=tmpCell[1];
res+=tmpCell[2];
used+=tmpCell[3];
}
printf(&quot;Subtot: %s\t%s\t%s\n\n&quot;, allo, res, used);
}
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Hi, I am so glad to have your reply, however I am having a little problem with the last script example.

I run the script against the txt file and I receive the following error:

/u01awk: 0602-586 Cannot read the value of arr. It is ana array name. The input number is 8. The file is test.txt.
The source line number is 16.

I did not make any changes and my text file is 8 lines long.
sorry to be a bother, any thoughts?

Again, many thanks
John
 
hi,
could we have a sample test.txt file here, pls. Also pls post the command line of how you run the script.

It did work for the posted data sample. vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top