I'm trying to create a bash script that ingests the output of another script cpu_latency.bt
The output of cpu_latency.bt is generated every second and looks similar to:
@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 |@@@@@@@@@@@@@@ |
I am trying to capture only the first number after the [ and then the number before the first | (so in the last line above that would be 8 and 4
The script below is fairly close (with the exception of handling [0] and [1] lines):
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 "\[")
However the output it returns is not quite right:
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"
Can recommend a better way of assigning the output of $1 and $3 to variables so I can write them out to a file?
Thanks CiCa
@RavinderSingh13 I'm not sure if I I'm misunderstanding how I can use an array for this, but a little more work with a while loop has gotten me considerably closer to what I'm aiming for:
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=""
}' )