Reputation: 130
I have an input file named test
which looks like this
leonid sergeevich vinogradov
ilya alexandrovich svintsov
and when I use grep like this grep 'leonid*vinogradov' test
it says nothing, but when I type grep 'leonid.*vinogradov' test
it gives me the first string. What's the difference between *
and .*
? Because I see no difference between any number of any characters and any character followed by any number of any characters.
I use ubuntu 14.04.3.
Upvotes: 0
Views: 45
Reputation: 5252
It's Regular Expression grep uses, short as regexp, not wildcards you thought. In this case, "." means any character, "" means any number of (include zero) the previous character, so "." means anything here.
Check the link, or google it, it's a powerful tool you'll find worth to knew.
Upvotes: 0
Reputation: 530882
*
doesn't match any number of characters, like in a file glob. It is an operator, which indicates 0 or more matches of the previous character. The regular expression leonid*vinogradov
would require a v
to appear immediately after 0 or more d
s. The .
is the regular expression metacharcter representing any single character, so .*
matches 0 or more arbitrary characters.
Upvotes: 1
Reputation: 784918
grep
uses regex and .*
matches 0 or more of any characters.
Where as 'leonid*vinogradov'
is also evaluated as regex and it means leoni
followed by 0 or more of letter d
hence your match fails.
Upvotes: 0