考虑iostat
$ iostat
Linux 2.6.31-20-generic-pae (ubuntu-9-10) 04/08/2010 _i686_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
2.38 0.07 0.66 0.39 0.00 96.51
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda 6.93 131.80 85.02 1347350 869177
如何将以sda
开头的行的值读入bash数组(忽略sda
字符串)?
答案 0 :(得分:3)
使用sed
查找该行并从中删除该字符串。使用$( ... )
将其输出捕获到变量。
$ sda=( $(iostat | sed -n '/^sda/ s/^sda// p') )
$ echo ${sda[0]}
6.93
$ echo ${sda[1]}
131.80
答案 1 :(得分:2)
试试这个:
x=($(iostat|grep -Po '^sda\s*\K.*'))
用你的例子测试:
kent$ cat f
Linux 2.6.31-20-generic-pae (ubuntu-9-10) 04/08/2010 _i686_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
2.38 0.07 0.66 0.39 0.00 96.51
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda 6.93 131.80 85.02 1347350 869177
kent$ x=($(cat f|grep -Po '^sda\s*\K.*'))
kent$ echo ${#x}
5
kent$ echo ${x[3]}
85.02
kent$ echo ${x[@]}
6.93 131.80 85.02 1347350 869177