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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

two dimensional arrays with sum from $3 2

Status
Not open for further replies.

meels

Technical User
May 10, 2002
5
0
0
CA
Hello,

I have the following input file:

From To len
===== ========== ===
===== ========== ===
210.255.0.1 10.0.0.101 70
210.255.0.2 10.0.0.103 71
210.255.0.1 10.0.0.101 100
210.255.0.2 10.0.0.105 72
210.255.0.1 10.0.0.115 73
210.255.0.1 10.0.0.115 74
210.255.0.3 10.0.0.105 75
210.255.0.2 10.0.0.112 76
210.255.0.1 10.0.0.111 77
210.255.0.3 10.0.0.105 2000
210.255.0.2 10.0.0.105 305

I need the following output

Source Destination No'' of Bytes
------ ----------- -------------
210.255.0.1 10.0.0.101 170
210.255.0.2 10.0.0.103 71
210.255.0.2 10.0.0.105 377
210.255.0.1 10.0.0.115 147
210.255.0.1 10.0.0.111 77
210.255.0.2 10.0.0.112 76
210.255.0.3 10.0.0.105 2075

Where the No'' of Bytes is the sum from each source/destination unique combination.

I am trying the following code but the output concatenatee the source and destination as this (210.255.0.110.0.0.101)
{
arr[$5,$6] +=$12
}

END {
for ( i in arr )
printf(" %s ---------- %s = %d\n",i,arr);
}

I tried to use the split (i, srcdst, "\034") it was fine but affected the No'' of bytes field.

As a aummary I need the following:

1-Get rid of the first three lines on the input file replace them by
Source Destination No'' of Bytes
------ ----------- -------------

2-Each unique combination of varaibles [$1,$2]
3-Put the accumulated value in the $3 on the No'' of Bytes field.

Thanks in advance,
meels






 
Something like this ?
Code:
# meels.awk
BEGIN{
printf "Source          Destination      No'' of Bytes\n"
printf "------          -----------      -------------\n"
}
/^[1-9]/{
  if($1!=src || $2!=dst){
    if(tot) printf "%-16s %-16s %10d\n",src,dst,tot
    tot=0;src=$1;dst=$2
  }
  tot+=$3
}
END{if(tot) printf "%-16s %-16s %10d\n",src,dst,tot}
sort /path/to/inputfile | awk -f meels.awk


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Code:
FNR > 3 {
   arr[$1, $2] += $3
}

END {
   printf("%-15s%-15s%-15s\n", "Source", "Destination", "No of Bytes");
   printf("%-15s%-15s%-15s\n", "----", "----", "----");
   for (i in arr) {
       split(i, sa, SUBSEP);
       printf("%-15s%-15s%-15s\n", sa[1], sa[2], arr[i]);
   }
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hello vgersh99 and PH,

The code from vgersh99 worked perfectly with nawk. Thanks alot.

The code from PH worked on awk and nawk but didn't give accurate output in either. Here is the output.

Source Destination No'' of Bytes
------ ----------- -------------
210.255.0.1 10.0.0.101 70
210.255.0.2 10.0.0.103 71
210.255.0.1 10.0.0.101 100
210.255.0.2 10.0.0.105 72
210.255.0.1 10.0.0.115 147
210.255.0.3 10.0.0.105 75
210.255.0.2 10.0.0.112 76
210.255.0.1 10.0.0.111 77
210.255.0.3 10.0.0.105 2000
210.255.0.2 10.0.0.105 305

Indeed, the unix based router I have supports awk only (not lucky). And I would like to have it on awk with accurate output.

One more thing I want to add is that the final output to be arranged up to down starting from the largest figure on No'' of Bytes field.

Thanks for your help. God bless you.
meels.
 
The code from PH worked on awk and nawk but didn't give accurate output
PHV said:
sort /path/to/inputfile | awk -f meels.awk
I guess you didn't sort as I suggested
up to down starting from the largest figure on No'' of Bytes field
Code:
# meels.awk
BEGIN{out="sort -n +2r"
printf "Source          Destination      No'' of Bytes\n"
printf "------          -----------      -------------\n"
}
/^[1-9]/{
  if($1!=src || $2!=dst){
    if(tot) printf "%-16s %-16s %10d\n",src,dst,tot | out
    tot=0;src=$1;dst=$2
  }
  tot+=$3
}
END{if(tot) printf "%-16s %-16s %10d\n",src,dst,tot | out}
[highlight]sort /path/to/inputfile | [/highlight]awk -f meels.awk

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PH,

It was my mistake. A sort at the begining. I understand about 80 % of the body but confused a bit. Could you explain couple of lines especially if(...) anf if(tot).

Thanks again,
meels
 
if(tot) is a shortway to say if(tot!=0), i.e. have we already found data to totalize ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top