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

4

2 回答 2

0

不确定 \s 是否适用于 grep。你可以试试 "^[ ] #. " 吗?在 [] 中有一个空格和一个制表符,两个字符。

于 2013-06-10T02:28:33.837 回答
0

我相信您缺少的是与一个或多个英镑符号匹配的加号。我在测试时没有 grep,但这看起来不错。我已经为我的测试提供了一个永久链接。

^\s*#+.*$

这是 regexpal.com 上测试的永久链接。

于 2013-06-09T14:08:09.523 回答