Is \d not supported by grep's basic expressions?

Ankur Agarwal picture Ankur Agarwal · Aug 1, 2011 · Viewed 37.8k times · Source

This does not generate any output. How come?

$ echo 'this 1 2 3' | grep '\d\+'

But these do:

$ echo 'this 1 2 3' | grep '\s\+'
this 1 2 3

$ echo 'this 1 2 3' | grep '\w\+'
this 1 2 3

Answer

Daenyth picture Daenyth · Aug 1, 2011

grep's default mode is (iirc) POSIX regex, and \d is pcre. You can either pass -P to gnu grep, for perl-like regexps, or use [[:digit:]] instead of \d.

daenyth@Bragi ~ $ echo 1 | grep -P '\d'
1
daenyth@Bragi ~ $ echo 1 | grep '[[:digit:]]'
1