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
Note: The sed
command at the bottom of this answer works only as long as your strings are nice behaving strings like
"foo"
or
'bar'
As soon as your strings start to misbehave :) like:
"hello \"world\""
it won't work any more.
Your input looks like source code. For a stable solution I recommend to use a parser for that language to extract the strings.
For trivial use cases:
You can use sed
. The solution is supposed to work on any POSIX platform in contrast to grep -oP
which only works with GNU grep:
sed -n 's/hello\.this_is(\(["'\'']\)\([^"]*\)\(["'\'']\).*/\2/gp' file
# ^^^^^^^^ ^^
# capture group 2 ^