I want to grep the shortest match and the pattern should be something like:
<car ... model=BMW ...> ... ... ... </car>
... means any character and the input is multiple lines.
You're looking for a non-greedy (or lazy) match. To get a non-greedy match in regular expressions you need to use the modifier ? after the quantifier. For example you can change .* to .*?.
?
.*
.*?
By default grep doesn't support non-greedy modifiers, but you can use grep -P to use the Perl syntax.
grep
grep -P
2.1m questions
2.1m answers
60 comments
57.0k users