Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading