brewmaster
brewmaster

Reputation: 130

grep wildcards issue ubuntu

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

Answers (3)

Tyl
Tyl

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

chepner
chepner

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 ds. The . is the regular expression metacharcter representing any single character, so .* matches 0 or more arbitrary characters.

Upvotes: 1

anubhava
anubhava

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

Related Questions