如何使用命令 IOSTAT -DlR 1 2

how to awk the command IOSTAT -DlR 1 2

输入:

#iostat -DlR 1 2

输出:

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

期望的输出:

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

我想要想要的输出。

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=; bps="2"; tps="3"; bread=; bwrtn=}'

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

当我运行上面的命令时,我得到的是这个(这不是我想要的输出:

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
$ awk '
    !NF { f=0 }
    =="%tm" { if (!cnt++) print "Device", , , , ; f=1 }
    f && /^[^[:space:]]/ { print , , , ,  }
' 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