Matching a sentence with grep
I'm trying to grep for the full sentence containing a search term. I've tried
grep (^.|\.\s).*searchterm.*(\.\s|\n)
but it's not working and I'm not sure why.
To clarify: I want stdout to print the full sentence of the search term. I am using grep to search through a single text file.
As an example, if my file has
"Foo blah. Blah blah searchterm blah blah. Foo bar."
I want stdout to print Blah blah searchterm blah blah
Tried this on my sh
-compatible terminal:
$ grep --only-matching --perl-regexp "[^.]*searchterm[^.]*" \
<<< "Foo blah. Blah blah searchterm blah blah. Foo bar."
Blah blah searchterm blah blah
$
Can be abbreviated to grep -oP
.
I think the problem with the regex you provided is specifying .*
to how greedy you wanted it to be (as stated by bertieb). What I did was just reformulate your request from "anything as long as it ends with dot" to "anything that's not a dot"