how to specify start and stop of a search string

I am trying to extract a string from a line of text. Currently I am using

grep -o 'startofstring(.........'

The string is not always the same size.
The string I'm trying to extract starts with 'test(' ends with ')'.

ex "blah,blah,blah,test(stringoftext),blah blah"

How do I specify to grep to start with 'test(' and stop with ')' ????

Also, sometimes this string exists more than once in the same text string.
How do I restrict it to only match on the first one ?

Thanks.

Try:

egrep -m1 -o "[a-z]+\([^)]+\)" file | head -1

Gee, what took you so long ? :smiley:

That sort of works. It ends at the ')', but it does not extract the string starting at the beginning '('. I'm getting everything from teh start of the line, through to the first ')'.

Can you post input that is producing undesired result and the desired output?

I figured it out by tweaking the code you gave me.
I changed the [a-z] to be the beginning of the text I want.
Maybe that was your intent ?

Anyway, it works now.
Thank you very much.

---------- Post updated at 01:46 PM ---------- Previous update was at 01:45 PM ----------

Here is how I tweaked it

egrep -m1 -o test"\([^)]+\)"

So the extracted string starts with "test(", and pulls everything up to the ")".

1 Like