2013-04-24 51 views
1

我想使用grep -v -e''从文件中排除注释(带有#的行作为第一个非空白字符)。 #可以出现在行的开头,也可以在遇到第一个#之前以任意组合形式出现多个空格和制表符的组合。正则表达式排除注释

假设文件NP4包含此:

# hash at the begining of the line 
## two hashes at the begining of the line 
#### four hashes at the begining of the line 
    # two white spaces then a hash 
a good line 
    another good line starting with a few spaces 
         a good line starting with a combination of spaces and tabs 
       # two white spaces, two tabes and then a hash 
        ## two tabs, two white spaces and then two hashes 
      # tab, ws, tab, ws, tab then hash 

我试着用下面的命令,但我认为这将不起作用。我只能得到三行作为输出。

的grep -v -e '^ \ S *#* $' NP4

回答

0

我相信你缺少的是加号,以匹配一个或多个英镑的象征。我没有grep,因为我测试了,但这看起来不错。我为我的测试提供了固定链接。

^\ S *#+。* $

Here is a permalink to the test on regexpal.com.

0

不知道\ S行之有效的grep的。 你可以试试“^ [] #。”? 在[]中有一个空格和一个制表符,两个字符。