regexsed

Sed to extract substring surrounded by strings


I am trying to use sed to extract a substring from a string, where the substring is surrounded by other strings.

Here's an example string:

Test: 1000 calls, 15307 milliseconds, 11 minimum, 37 maximum, 15 average, top five [37,35,34,32,31]

and I want to extract the "average" value (in the example above, that would be 15.

I have tried:

echo "Test: 1000 calls, 15307 milliseconds, 11 minimum, 37 maximum, 15 average, top five [37,35,34,32,31]" | sed 's/.*\(maximum,.*\)average/\1/'

but the output I am getting is:

maximum, 15 , top five [37,35,34,32,31]

How can I NOT include the "maximum, " that is in front of the "15", and NOT include the part of the string that is after the "15"?

EDIT: Apologies I messed up and used wrong example - I will correct it in a bit :( !

EDIT 2: I have correct the example.


Solution

  • sed is not the best tool to extract substrings, but in this case, it can be used:

    echo "Test: 1000 calls, 15307 milliseconds, 11 minimum, 37 maximum, 15 average, top five [37,35,34,32,31]" \
    | sed 's/.* \([0-9]*\) average,.*/\1/'
    

    I.e. match everything up to a number before average, remember the number, and replace everything with the number.