2016-02-13 170 views

回答

2

一个好方法是用negated character class

就像[abc]将匹配ab,或c[^abc]将匹配不ab,或c任何字符。 ^将正则表达式定位在行的开始位置,所以它后面必须匹配字符串的第一个字符。

$ cat test 
does not start with space 
starts with space 
     starts with more spaces 
another good line 

$ egrep '^[^ ]' test 
does not start with space 
another good line 

如果我们想跳过开头的行空白,包括标签,你可以使用字符类中的特殊[:space:]括号表达式:

egrep '^[^[:space:]]' test 

如果你是不是在找其他的东西行,你也可以使用倒匹配

-v, --invert-match 
     Selected lines are those not matching any of the specified pat- 
     terns. 

因此,我们可以这样做,而不是:

egrep -v '^[[:space:]]' test 

grep应该这样做太:

grep -v '^[[:space:]]' test 
1

可以使用-v开关跳过匹配的行。

egrep -v '^ ' 

man egrep