2013-10-18 27 views
2

我试图用\s来指定,而空格grep正则表达式,但似乎grep无法识别它。 grep -Eegrep也没有与它一起工作。在grep中支持正则表达式转义序列

这里是我的考验:

$ echo ' foo' | grep '^ *foo' 
    foo 
$ echo ' foo' | grep '^\s*foo' <-- **No Output** 
$ echo ' foo' | grep -E '^\s*foo' <-- **No Output** 
$ echo ' foo' | egrep '^\s*foo' <-- **No Output** 
$ echo ' foo' | grep '^[[:space:]]*foo' 
    foo 

我也注意到,它承认\w但不\d

阙1.是否有可能使grep识别\s

阙2.我在哪里可以找到支持grep的正则表达式转义序列的列表?

OSRHEL5

壳牌:尝试shbashtcsh

$ grep --version 
grep (GNU grep) 2.5.1 

编辑

我试着用grep -P作为答案的建议,但遗憾的是它没有牛逼的工作对我的系统抛出以下错误:

$ echo ' foo' | grep -P '^\s*foo' 
grep: The -P option is not supported <-- **ERROR** 
+0

仅供参考......在Perl的grep()接受\ S。我已经在PERL中尝试了下面的程序,它工作正常。'我的@Arr =(“Test”,“Test”); @Arr = grep {$ _ =〜/^\ s + Test /} @Arr; print“@Arr”; '输出:“测试” – Anand

+0

@Anand是的。我注意到PERL中的grep工作正常。我的意图是让它在linux shell上工作 – jkshah

回答

2

没有grep的egrep的或不承认\s\d(PCRE特定的结构),因为它使用ERE。如果你使用grep -P那么所有PCRE功能被认为

POSIX and ERE Regex

虽然:请参阅本参考指南。

例如

echo ' foo' | grep -P '^\s*foo' 
    foo 
+0

不幸的是'-P'选项在我的系统上不起作用。 'grep:-P选项不支持' – jkshah

+0

已更新的操作系统和shell问题试过 – jkshah

+0

@jkshah:我确定你可以在RHEL上通过RPM获得gnu grep – anubhava

2

Que 1. Is it possible to make grep recognize \s ?

使用Perl的正则表达式模式:

echo ' foo' | grep -P '^\s*foo' 

Que 2. Where do I find a list supported regex escape sequences in grep ?

man grep 

有它说,例如:

The symbols \< and > respectively match the empty string at the beginning and end of a word. The symbol \b matches the empty string at the edge of a word, and \B matches the empty string provided it's not at the edge of a word. The symbol \w is a synonym for [[:alnum:]] and \W is a synonym for [^[:alnum:]].

没有其他[A-ZA-Z]被提及

版本:

grep (GNU grep) 2.14 

在Debian

+0

不幸的是-P选项在我的系统上无法正常工作。 grep:-P选项不受支持。更新的问题 – jkshah