Assigning output from awk to variables

CiCa

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=""
}' )
Ed Morton

Is this what you're trying to do?

$ 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python: Assigning the second value from a line of output to variable

Swift is not assigning variables from JSON results

Assigning output of for loop as variables in dynamic list

Assigning variables from dataframe

Assigning specific data from another webpage to variables or an array

Retrieving a Record from a File and Assigning to Variables

awk with variables in condition and in output redirection file

Assigning Dynamic SQL Output Variables to Local Variables

Assigning Values to Class Variables From an Input String

awk not assigning strings or substrings

No output from inotifywait | awk

Assigning variables?

Assigning variables from text in filename

Assigning variables from async method

Assigning variables from a list, or "how to make an elegant save function"

Assigning Final variables from constructors | not allowed from methods. Why?

Assigning Variables from Array in MySQL Database

System cannot poweroff when assigning commands' output to variables

Assigning match variables from nth match

Set multiple variables to different fields of awk output

C# Assigning items from params to variables based on class

Assigning values from list to variables in Python

Copying variables to local text file from multiple ssh awk output

Assigning Environment Variables from Consul KV using consul-template

Assigning values from an array into variables in javascript

Adding two numbers from list and assigning the output to a matrix

Assigning function output to many variables: good or bad practice

Reading lines from file and assigning content to variables

Efficiently assigning multiple variables created from a subset of grouped data in R

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive