• Uncategorized

About linux : Is-\d-not-supported-by-greps-basic-expressions-closed

Question Detail

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

Question Answer

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

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

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.