how to awk the command IOSTAT -DlR 1 2

219 views Asked by At

input:

#iostat -DlR 1 2

Output:

root@myserver #iostat -DlR 1 2

System configuration: lcpu=20 drives=2 paths=16 vdisks=0

Disks:                      xfers                                read                                write                                  queue                  
--------------- -------------------------------- ------------------------------------ ------------------------------------ -------------------------------------- 
                  %tm    bps   tps  bread  bwrtn   rps    avg    min    max time fail   wps    avg    min    max time fail    avg    min    max   avg   avg  serv
                  act                                    serv   serv   serv outs              serv   serv   serv outs        time   time   time  wqsz  sqsz qfull
hdisk1            0.0   0.0    0.0   0.0    0.0    0.0   0.0    0.0    0.0     0    0   0.0   0.0    0.0    0.0     0    0   0.0    0.0    0.0    0.0   0.0   0.0
hdisk0            0.0   0.0    0.0   0.0    0.0    0.0   0.0    0.0    0.0     0    0   0.0   0.0    0.0    0.0     0    0   0.0    0.0    0.0    0.0   0.0   0.0

Disks:                      xfers                                read                                write                                  queue                  
--------------- -------------------------------- ------------------------------------ ------------------------------------ -------------------------------------- 
                  %tm    bps   tps  bread  bwrtn   rps    avg    min    max time fail   wps    avg    min    max time fail    avg    min    max   avg   avg  serv
                  act                                    serv   serv   serv outs              serv   serv   serv outs        time   time   time  wqsz  sqsz qfull
hdisk1            0.0   0.0    0.0   0.0    0.0    0.0   0.0    0.0    0.0     0    0   0.0   0.0    0.0    0.0     0    0   0.0    0.0    0.0    0.0   0.0   0.0
hdisk0            0.0  77.8K  19.0   0.0   77.8K   0.0   0.0    0.0    0.0     0    0  19.0   0.3    0.2    0.5     0    0   0.0    0.0    0.0    0.0   0.0   0.0

Desired Output:

Device     bps     tps     bread     bwrtn
hdisk1     0.0     0.0      0.0       0.0         
hdisk0     0.0     0.0      0.0       0.0
hdisk1     0.0     0.0      0.0       0.0         
hdisk0    77.8k   19.0      0.0      77.8K

I want the desired output.

HEADER='Device      bps     tps     bread     bwrtn'
HEADERIZE="BEGIN {print \"$HEADER\"}"
PRINTF='{printf "%s  %s  %s  %s  %s  %s  %s  %s\n", device, bps, tps, bread, bwrtn}'
CMD='iostat -DlR 1 2'
FILTER='/^cd/ {next} /^Disks:/ {reportOrd++; next} (reportOrd<2) {next}'
FORMAT='{device=$1; bps="2"; tps="3"; bread=$4; bwrtn=$5}'

$CMD | tee $TEE_DEST | awk "$HEADERIZE $FILTER $FORMAT $PRINTF"  header="$HEADER"

When I run the above command the out Im getting is this (which is not my desired output:

root@myserver #$CMD | tee $TEE_DEST | awk "$HEADERIZE $FILTER $FORMAT $PRINTF"  header="$HEADER"
Device      bps     tps     bread     bwrtn
---------------            2            3  ------------------------------------  --------------------------------------
%tm                   2            3        bread        bwrtn
act                   2            3         serv         outs
hdisk1                2            3          0.0          0.0
hdisk0                2            3          0.0          0.0
1

There are 1 answers

0
Ed Morton On BEST ANSWER
$ awk '
    !NF { f=0 }
    $1=="%tm" { if (!cnt++) print "Device", $2, $3, $4, $5; f=1 }
    f && /^[^[:space:]]/ { print $1, $3, $4, $5, $6 }
' file | column -t
Device  bps    tps   bread  bwrtn
hdisk1  0.0    0.0   0.0    0.0
hdisk0  0.0    0.0   0.0    0.0
hdisk1  0.0    0.0   0.0    0.0
hdisk0  77.8K  19.0  0.0    77.8K