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

LinuxBashGrep

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

As specified in POSIX, grep uses basic regular expressions, but \d is part of a Perl-compatible regular expression (PCRE).

If you are using GNU grep, you can use the -P option, to allow use of PCRE regular expressions. Otherwise you can use the POSIX-specified [[:digit:]] character class in place of \d.

echo 1 | grep -P '\d'
# output: 1
echo 1 | grep '[[:digit:]]'
# output: 1

Solution 2 - Linux

Try this $ echo 'this 1 2 3' | grep '[0-9]\+'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnkur AgarwalView Question on Stackoverflow
Solution 1 - LinuxDaenythView Answer on Stackoverflow
Solution 2 - LinuxCharles MaView Answer on Stackoverflow