3

I need to grep words like these:

ABC-DEF
AB2-DEF
AB3-DEF
AB-DEF

So I was trying:

grep AB*-DEF
grep -w -AB*-DEF
grep -w AB*DEF

But neither of them are working.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
GP92
  • 815

4 Answers4

13

* in a regex is not like a filename glob. It means 0 or more of the previous character/pattern. So your examples would be looking for a A then 0 or more B then -DEF

. in regex means "any character" so you could fix your pattern by using

grep 'AB.*DEF'
Eric Renouf
  • 18,431
3

As far as your patterns are concerned, this would be the safest to match only intended strings:

grep 'AB.\{0,1\}-DEF' file.txt

Or

grep -E 'AB.?-DEF' file.txt

. matches any single character, ? and \{0,1\} matches the previous token zero or one time, so in total .? and .\{0,1\}will match zero or one character before -DEF.

If you use AB.*-DEF or AB.*DEF, grep will greedily match unintended strings, for example:

ABCG-DEF
ABCGLHHJKH90-DEF
heemayl
  • 56,300
1

You can use:

grep 'AB.*-DEF' file.txt
cuonglm
  • 153,898
nghnam
  • 129
0

The wildcards in your regular expressions are expanded by the shell. The shell treats them as filename metacharacters. So, you have to tell the shell to not evaluate them as filename metacharacters and you do that by quoting them using single quotes, double quotes, or backslash character just before the metacharacter. Then, the shell sends the expression as such to the grep utility which handles the regular expression.

unxnut
  • 6,008