3

I ran grep 'd(o|i)g' a.txt and a.txt contains dig but nothing was returned. Is this the right way to match dog or dig?

zmkm
  • 141

3 Answers3

7

Yes, d(o|i)g is the correct way to do it. You can also do d[oi]g since you are dealing with single characters.

You need to use the -E flag on your grep call to get extended regexes.

$ cat a.txt
bird
dog
cat
dug
$ grep 'd(o|i)g' a.txt
$ grep -E 'd(o|i)g' a.txt
dog
  • 4
    Standard way of doing it for this particular case: grep -e dog -e dig a.txt or grep 'd[io]g' a.txt, as you propose. – Kusalananda Feb 21 '22 at 19:03
5

You need to escape the regex meta characters, like

> echo dig | grep "d\(o\|i\)g"
dig

, or you can switch to ERE ("extended RegEx"es):

echo dig | grep -E "d(o|i)g"
dig
RudiC
  • 8,969
3

You could also try grep 'd[io]g' but this only works for single characters.

user10489
  • 6,740