Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>awk -v tp1=$tp1 -v tp2=$tp2 '$1 &gt; tp1 &amp;&amp; $1 &lt; tp2 { total[$2] += $3; } END { for (ip in total) print "IP:", ip, "=&gt;", total[ip]; }' </code></pre> <p>seems simple enough. If that doesn't work for you, in what way doesn't it work?</p> <p><em>EDIT: Reformatted. Indentation is being ignored, don't know why.</em></p> <p>Explanation:</p> <ul> <li><code>-v tp1=$tp1</code> means "create variable in awk named <i>tp1</i> with the value of the shell variable named <i>tp1</i>.</li> <li><code>$1 &gt; tp1 &amp;&amp; $1 &lt; tp2</code> means "only execute this code block if the value of the first field is larger than the value of <i>tp1</i> and smaller than the value of <i>tp2</i>.</li> <li><code>total[$2] += $3;</code> uses the value of the second field (the IP address) as an array index into an implicitly declared array named "tota", and adds the value of the third field.</li> <li>AWK will iterate over all records in the input, with the default record being a single line of text. For each record, the above code will be executed if the condition is valid.</li> <li><code>END</code> identifies a code block that is executed after all records have been processed, instead of being executed against every (matching) input record.</li> <li><code>for (ip in total)</code> identifies another iterator: For each value stored in the array named <code>total</code>, assign the name of the array index to the variable named <code>ip</code>, and execute the associated code block.</li> <li>In this case, the associated code block is <code>print "IP:", ip, "=>", total[ip];</code>, which prints the string "IP:", a space, the value if the variable named <code>ip</code> (which is an IP address, used as an array index name), another space, the string "=>", <i>yet</i> another space, and the value stored in the array <code>total</code> indexed by the IP address.</li> </ul> <p>Any questions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload