Ankur Agarwal
Ankur Agarwal

Reputation: 24778

grep with regexp: whitespace doesn't match unless I add an assertion

GNU grep 2.5.4 on bash 4.1.5(1) on Ubuntu 10.04

This matches

$ echo "this is a     line" | grep 'a[[:space:]]\+line'
this is a     line

But this doesn't

$ echo "this is a     line" | grep 'a\s\+line'

But this matches too

$ echo "this is a     line" | grep 'a\s\+\bline'
this is a     line

I don't understand why #2 does not match (whereas # 1 does) and #3 also shows a match. Whats the difference here?

Upvotes: 9

Views: 21632

Answers (2)

David W.
David W.

Reputation: 107090

Take a look at your grep manpage. Perl added a lot of regular expression extensions that weren't in the original specification. However, because they proved so useful, many programs adopted them.

Unfortunately, grep is sometimes stuck in the past because you want to make sure your grep command remains compatible with older versions of grep.

Some systems have egrep with some extensions. Others allow you to use grep -E to get them. Still others have a grep -P that allows you to use Perl extensions. I believe Linux systems' grep command can use the -P extension which is not available in most Unix systems unless someone has replaced the grep with the GNU version. Newer versions of Mac OS X also support the -P switch, but not older versions.

Upvotes: 4

dogbane
dogbane

Reputation: 274828

grep doesn't support the complete set of regular expressions, so try using -P to enable perl regular expressions. You don't need to escape the + i.e.

echo "this is a     line" | grep -P 'a\s+line' 

Upvotes: 4

Related Questions