Let's say I have the following string:
hello hi this test is test a test greeting test hello hello
If I do grep -o "hi.*test"
, I will get:
hi this test is test a test greeting test
It greps everything between the first hit of 'hi' and the last hit of 'test'. How can I make it so that it stops grepping at the first hit. AKA:
If I do grep -o "hi.*test"
(ofcourse with an extra flag or a different command), I will get:
hi this test
Instead of:
hi this test is test a test greeting test
It needs to stop at the first 'test', not the last.
How do I do that? Thanks!
EDIT to clarify: If I have the following:
hello this is a test hello this is a test
And I do grep "hello.*test", I will get the complete string. But I want:
hello this is a test
hello this is a test
AKA it found the string two times AKA it searched for "hello this is a test" and found it twice
You can do this using PCRE mode (
grep -P
) by using the Perl non-greedy modifier?
: