Bash to extract number followed by a specific string and a :

Advertisements

I have this file with number of

Timing=Time1:5/1,Time2:3/1,Time3:4/1,Time4:4/1
Timing=Time1:1/1,Time2:3/1,Time3:5/1,Time4:9/1
...

In a metrics file.

For format here is Time1 is the name of the metric, the next number is the number of time units it took and the number followed by the slash is a number denoting what kind of time unit it is (1 is millis).

I am trying to parse out all the instances of the value of Time3 from this list of times.

So in the above example I am trying to parse out

4
5

How would I accomplish doing that through bash/awk/sed/grep etc?

>Solution :

With just GNU :

$ grep -oP 'Time3:\K\d+(?=/\d)' file
4
5

If you think the look ahead is not necessary, feel free to remove it:

 $ grep -oP 'Time3:\K\d+

The regular expression matches as follows:

Node Explanation
Time3: ‘Time3:’
\K resets the start of the match (what is Kept) as a shorter alternative to using a look-behind assertion: look arounds and Support of K in regex
\d+ digits (0-9) (1 or more times (matching the most amount possible))
(?= look ahead to see if there is:
/ /
\d digits (0-9)
) end of look-ahead

Leave a ReplyCancel reply