This page says that any multiplier in a regex can be suffixed with '?' to do a non greedy search. However, I can't figure out how to make it work in grep.
grep -E '^.*?$' file
prints the whole text.
How can I do a non greedy regex search with grep?
^.*?$
will match the same thing as^.*$
as that's constrained by the^
and$
. Non greedy operators are not useful forgrep
(that print the matching lines, unless you use non-standard extensions like GNU's-o
) as they don't affect which lines do match the pattern, just what is being matched inside the line. – Stéphane Chazelas May 28 '17 at 06:49grep -o
is somewhat common even if nonstandard, and a non-greedy match might come useful there. – ilkkachu May 28 '17 at 08:42