I have a series of strings I want to extract:
hello.this_is("bla bla bla")
some random text
hello.this_is('hello hello')
other stuff
What I need to get (from many files, but this is not important here) is the content between hello.this_is(
and )
, so my desired output is:
bla bla bla
hello hello
As you see, the text within parentheses can be enclosed with either double or single quotes.
If this was only single quotes I would use a look behind and look ahead just like this:
grep -Po "(?<=hello.this_is\(').*(?=')" file
# ^ ^
# returns ---> hello hello
Similarly, to get strings from double quotes I would say:
grep -Po '(?<=hello.this_is\(").*(?=")' file
# ^ ^
# returns ---> bla bla bla
However, I want to match both cases, so it gets both single and double quotes. I tried with using $''
to escape, but could not make it work:
grep -Po '(?<=hello.this_is\($'["\']').*(?=$'["\']')' file
# ^^^^^^^^ ^^^^^^^^
I can of course use the ASCII number and say:
grep -Po '(?<=hello.this_is\([\047\042]).*' file
but I would like to use the quotes and single quotes, since 047
and 042
are not that much representative to me as single and double quotes are.