将 awk 的输出分配给变量

我正在尝试创建一个 bash 脚本来摄取另一个脚本的输出 cpu_latency.bt

的输出cpu_latency.bt每秒生成一次,类似于:

@usecs: 
[0]                    3 |@@@@@@@@@@                                          |
[1]                    5 |@@@@@@@@@@@@@@@@@                                   |
[2, 4)                 5 |@@@@@@@@@@@@@@@@@                                   |
[4, 8)                 0 |                                                    |
[8, 16)                5 |@@@@@@@@@@@@@@@@@                                   |
[16, 32)              15 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[32, 64)               1 |@@@                                                 |
[64, 128)              0 |                                                    |
[128, 256)             1 |@@@                                                 |

@usecs: 
[0]                    1 |@@@                                                 |
[1]                    1 |@@@                                                 |
[2, 4)                 6 |@@@@@@@@@@@@@@@@@@@@@@                              |
[4, 8)                 2 |@@@@@@@                                             |
[8, 16)                4 |@@@@@@@@@@@@@@                                      |

我试图捕获第一个数字之后的第一个数字[,然后是第一个数字之前的数字|(所以在上面的最后一行是84

下面的脚本相当接近(处理[0][1]行除外):

duration=10
while read line
do
        echo $line | cut -d "|" -f1 | sed 's/\[//g; s/\,//g; s/)//g' | awk '{print $1,$3}' |
        while read key value; do
                print "The Key is "$key "and the Value is "$value
        done

done < <(timeout $duration cpu_latency.bt | grep "\[")

但是它返回的输出不太正确:

Error: no such file "The Key is 16"
Error: no such file "and the Value is 10"
Error: no such file "The Key is 16"
Error: no such file "and the Value is 9"

可以推荐一种更好的方法将 $1 和 $3 的输出分配给变量,以便我可以将它们写到文件中吗?

感谢 CiCa


@RavinderSingh13我不确定我是否误解了如何为此使用数组,但是使用while循环进行的更多工作使我更接近我的目标:

while read key value
do  
    echo `hostname`".cpu-lat."$key"\\"$value"\\`date +"%s"`"  >> /tmp/stats.out
done < <(
timeout $duration /root/bpftrace/cpu_latency.bt | awk '
match($0,/^\[[0-9]+/){
  val=substr($0,RSTART+1,RLENGTH-1)
  match($0,/[0-9]+ +\|/)
  val2=substr($0,RSTART,RLENGTH)
  sub(/ +\|/,"",val2)
  print val,val2
  val=val2=""
}' )
埃德·莫顿

这是你想要做的吗?

$ awk -F'[][,[:space:]]+' 'sub(/ \|.*/,""){print $2, $NF}' file
0 3
1 5
2 5
4 0
8 5
16 15
32 1
64 0
128 1
0 1
1 1
2 6
4 2
8 4

.

$ awk -v host="$(hostname)" -v date="$(date +%s)" -F'[][,[:space:]]+' '
    sub(/ \|.*/,"") { printf "%s.cpu-lat.%s\\%s\\%s\n", host, $2, $NF, date }
' file
mypc.cpu-lat.0\3\1576428453
mypc.cpu-lat.1\5\1576428453
mypc.cpu-lat.2\5\1576428453
mypc.cpu-lat.4\0\1576428453
mypc.cpu-lat.8\5\1576428453
mypc.cpu-lat.16\15\1576428453
mypc.cpu-lat.32\1\1576428453
mypc.cpu-lat.64\0\1576428453
mypc.cpu-lat.128\1\1576428453
mypc.cpu-lat.0\1\1576428453
mypc.cpu-lat.1\1\1576428453
mypc.cpu-lat.2\6\1576428453
mypc.cpu-lat.4\2\1576428453
mypc.cpu-lat.8\4\1576428453

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章