Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <code>bash</code> and <code>awk</code>:</p> <blockquote> <p><code>cat file | sed -ne 's:^.*INFO.*\[\([0-9, ]*\)\][ \r]*$:\1:p' | awk -F ' *, *' '{ sum2 += $2 ; sum3 += $3 } END { if (NR&gt;0) printf "avg2=%.2f, avg3=%.2f\n", sum2/NR, sum3/NR }'</code></p> </blockquote> <p>Sample output (for your original data):</p> <blockquote> <p><code>avg2=2859.59, avg3=149.94</code></p> </blockquote> <p>Of course, you do not need to use <code>cat</code>, it is included there for legibility and to illustrate the fact that input data can come from any pipe; if you have to operate on an existing file, run <code>sed -ne '...' file | ...</code> directly.</p> <hr> <p><strong>EDIT</strong></p> <p>If you have access to <code>gawk</code> (GNU awk), you can eliminate the need for <code>sed</code> as follows:</p> <blockquote> <p><code>cat file | gawk '{ if(match($0, /.*INFO.*\[([0-9, ]*)\][ \r]*$/, a)) { cnt++; split(a[1], b, / *, */); sum2+=b[2]; sum3+=b[3] } } END { if (cnt&gt;0) printf "avg2=%.2f, avg3=%.2f\n", sum2/cnt, sum3/cnt }'</code></p> </blockquote> <p>Same remarks re. <code>cat</code> apply.</p> <p>A bit of explanation:</p> <ul> <li><code>sed</code> only prints out lines (<code>-n ... :p</code> combination) that match the regular expression (lines containing INFO followed by any combination of digits, spaces and commas between square brackets at the end of the line, allowing for trailing spaces and CR); if any such line matches, only keep what's between the square brackets (<code>\1</code>, corresponding to what's between <code>\(...\)</code> in the regular expression) before printing (<code>:p</code>) <ul> <li>sed will output lines that look like: <code>8541, 931, 0, 0</code></li> </ul></li> <li><code>awk</code> uses a comma surrounded by 0 or more spaces (<code>-F ' *, *'</code>) as field delimiters; <code>$1</code> corresponds to the first column (e.g. 8541), <code>$2</code> to the second etc. Missing columns count as value <code>0</code> <ul> <li>at the end, <code>awk</code> divides the accumulators <code>sum2</code> etc by the number of records processed, <code>NR</code></li> </ul></li> <li><code>gawk</code> does everything in one shot; it will first test whether each line matches the same regular expression passed in the previous example to <code>sed</code> (except that unlike <code>sed</code>, <code>awk</code> does not require a <code>\</code> in fron the round brackets delimiting areas or interest). If the line matches, what's between the round brackets ends up in a[1], which we then split using the same separator (a comma surrounded by any number of spaces) and use that to accumulate. I introduced <code>cnt</code> instead of continuing to use <code>NR</code> because the number of records processed <code>NR</code> may be larger than the actual number of relevant records (<code>cnt</code>) if not all lines are of the form <code>INFO ... [...comma-separated-numbers...]</code>, which was not the case with <code>sed|awk</code> since <code>sed</code> guaranteed that all lines passed on to <code>awk</code> were relevant.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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