egrep matching expressions with wildcard

Going through some exercises, got stuck on this one:

egrep "s*as" states.txt

## Alaska
## Arkansas
## Kansas
## Massachusetts
## Nebraska

I can understand why it selects Arkansas, Kansas, Massachusetts, but why Alaska? There should have been the first "s" from "s*as", should it not? Am I missing something in plain sight? Sorry if the answer is obvious, I don't get it. grep gives the same results.

Upvotes: 0

Views: 1139

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

In your call to egrep below:

egrep "s*as" states.txt

The quantity s* means match s zero or more times. Hence, Alaska matches because it contains as. If you intend to match s, followed by any single character, followed by as then use dot:

egrep "s.as" states.txt

Note that there is a difference between filename wildcards and regular expressions.

* in regular expression, quoting GNU Grep manual:

The preceding item will be matched zero or more times


* in filename wildcard, quoting Bash Reference Manual:

Matches any string, including the null string

Upvotes: 2

Related Questions