Qn on awk greping values

Hello Experts,

I was trying to awk out some data out of a text file.
Below is a sample file which I have

xxx ***Wed Jun 28 18:00:59 CDT 2015
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          17.10    0.00    4.56    2.86    0.00   75.48

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00
sdd               0.00     0.00    1.00    1.00     0.50     0.50     1.00     0.00    1.00   1.00   0.20
sdf               0.00     0.00   28.00  910.00   865.00  9985.00    23.13     1.38    1.47   0.12  11.70
sde               0.00     0.00    0.00    2.00     0.00     4.00     4.00     0.00    1.00   1.00   0.20

xxx ***Wed Jun 28 18:01:59 CDT 2015
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          17.10    0.00    4.56    2.86    0.00   75.48

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00
sdd               0.00     0.00    1.00    1.00     0.50     0.50     1.00     0.00    1.00   1.00   0.20
sdf               0.00     0.00   28.00  910.00   865.00  9985.00    23.13     1.38    1.47   0.12  12.70
sde               0.00     0.00    0.00    2.00     0.00     4.00     4.00     0.00    1.00   1.00   0.20

I want an output like below

xxx ***Wed Jun 28 18:00:59 CDT 2015
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          17.10    0.00    4.56    2.86    0.00   75.48

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await  svctm  %util
sdf               0.00     0.00   28.00  910.00   865.00  9985.00    23.13     1.38    1.47   0.12  11.70

xxx ***Wed Jun 28 18:01:59 CDT 2015
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          17.10    0.00    4.56    2.86    0.00   75.48

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await  svctm  %util
sdf               0.00     0.00   28.00  910.00   865.00  9985.00    23.13     1.38    1.47   0.12  12.70

Basically, the awk is searching value on columns 1-12 >10 and print the whole line & the header lines.

I managed to get the command to grep the values on columns. But couldn't find a way to just print the 4 lines above the one which start with Device:

awk -F "[: ]" '{for (i=12;i<=NF;i++) if ($i>=50){print $0;next}}' infile

Thanks in advance
Dev

Your example is a bit odd.
And comparing all the columns with one number is a bit odd, too.
The following demonstrates how the header is stored in a buffer header and how control variables inheader and doheader control the collecting of the buffer and its printing, respectively.
The decision if its the header is done by the (NF<12) condition.

awk '(NF<12) {if (inheader) {header=(header RS $0)} else {header=$0; inheader=doheader=1} next} {inheader=0; for (i=2; i<=NF; i++) if ($i+0>=50) {if (doheader) {print header; doheader=0} print; break}}' infile

Or, as a multi-liner

awk '
(NF<12) {
  if (inheader) {
    header=(header RS $0)
  } else {
    header=$0
    inheader=doheader=1
  }
  next
}
{
  inheader=0
  for (i=2; i<=NF; i++)
    if ($i+0>=50) {
      if (doheader) {
        print header
        doheader=0
      }
      print
      break
    }
}' infile
1 Like

Thank you for the pointer. The output is from a script that runs unix IO stat every minute and spool to one single file. The script you provided is exactly doing what I wanted. After may days of unsuccessful efforts, glad that I could get some help here.

:b: