Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #154

Re: gnuplot algorithm help

Date 2011-04-11 06:11 -0400
From Mike Rhodes <M8R-1cd059@mailinator.com>
Newsgroups comp.lang.awk
Subject Re: gnuplot algorithm help
References <4da27014$0$669$c3e8da3$b280bf18@news.astraweb.com> <inue7l$oa7$1@speranza.aioe.org>
Message-ID <4da2d3c0$0$794$c3e8da3$e408f015@news.astraweb.com> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


On 4/11/11 4:30 AM, Janis Papanagnou wrote:
> Am 11.04.2011 05:05, schrieb Mike Rhodes:
>> Hello fellow AWKers,
>>
>> I'm trying to use output from nfdump to plot total throughput in gnuplot
>> but I am having trouble coming up with the correct algorithm.
>>
>> Given a datafile organized this way...
>>
>> # START_TIME   MBPS     DUR
>> 1302350771.016 0.003040 1.752
>> 1302350788.681 0.047432 4.548
>> 1302350792.845 0.032472 1.120
>> 1302350792.901 0.021536 0.172
>> 1302350793.217 0.023680 0.420
>> 1302350800.717 0.025024 0.492
>> 1302350814.777 0.001248 0.000
>> 1302350814.781 0.001248 0.020
>> 1302350823.701 0.019568 0.228
>> 1302350830.432 0.000832 0.096
>>
>> ...I'm trying to keep a running total of Mbps when two times overlap. In
>> other words, when $1 is less than any previous $1+$3 then $2 should be
>> added to the $2 of all of the the records with the larger $1+$3.
> 
> IIUC, here's one way (without introducing getline)...
> 
> /^#/ { next }
> $1 < s { f2 += $2 }
> s && $1 >= s { print f1, f2, f3 }
> { f1 = $1; f2 = $2; f3 = $3; s = $1 + $3 }
> END { print f1, f2, f3 }

I shouldn't have said "running total." That's not exactly what I'm
after. Rather, $1 and $3 should stay the same, but when $1 is greater
than any previous $1+$3 that record's $2 should be the sum of its $2 and
those previous $2's. So given the data set above, this would be the result:

1302350771.016 0.003040 1.752
1302350788.681 0.047432 4.548
1302350792.845 0.079904 1.120
1302350792.901 0.148872 0.172
1302350793.217 0.151016 0.420
1302350800.717 0.025024 0.492
1302350814.777 0.001248 0.000
1302350814.781 0.001248 0.020
1302350823.701 0.019568 0.228
1302350830.432 0.000832 0.096

Note that lines 3, 4, and 5 have different $2 values than the original set.

> 
> 
>>
>> Here's what I've come up with so far:
>>
>> {
>>      print
>>      longest = $1 + $3
>>      tput = $2
>>      getline
> 
> This getline interfers with normal builtin looping.

Right. That's the problem I'm having. But I need to read in the next
line in order to compare it to the current one.

> 
>>      while ($1<  longest) {
>>          tput += $2
>>          print $1, tput, $3
>>          getline
>>      }
>>      print
>> }
> 
> What you seem to be trying to do is re-implementing awk's builtin
> looping. You probably wanted to do something like
> 
> BEGIN { ...
>    while (getline...) {
>       ...
>       while (...) {
>         getline...
>       ...
>       }
>    }
>    ...
> }
> 
> But I suggest to not use getline.
> 
>>
>> The obvious flaw here is that two lines are printed in a row without any
>> tests being applied when the while loop is broken. I've been unable to
>> come up with an algorithm that allows me to do the comparison I need but
>> doesn't swallow any output via unprinted getlines. I know that getline
>> is considered an "advanced" function, but I'm hoping someone here has
>> enough experience with it to help me on this.
> 
> Getline is not an advanced function, it's a standard function that
> has some caveats to consider. What you probably need is not someone
> who has experience with getline. Just try to use awk's normal looping
> and save intermediate results as necessary.

--Mike

Back to comp.lang.awk | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

gnuplot algorithm help Mike Rhodes <M8R-1cd059@mailinator.com> - 2011-04-10 23:05 -0400
  Re: gnuplot algorithm help Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-04-11 10:30 +0200
    Re: gnuplot algorithm help Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-04-11 12:06 +0200
    Re: gnuplot algorithm help Mike Rhodes <M8R-1cd059@mailinator.com> - 2011-04-11 06:11 -0400
      Re: gnuplot algorithm help Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-04-11 14:40 +0200
        Re: gnuplot algorithm help Mike Rhodes <M8R-1cd059@mailinator.com> - 2011-04-11 12:29 -0400
          Re: gnuplot algorithm help Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-04-11 19:51 +0200
            Re: gnuplot algorithm help Mike Rhodes <M8R-1cd059@mailinator.com> - 2011-04-11 19:42 -0400

csiph-web