小编use*_*987的帖子

为什么 `^[ ]{0,}` 不能与 Linux grep 一起使用?

这是我的示例文本。grep wgrep ^w并且grep '^[ ]w'工作得很好。

[user@linux ~]$ grep w text.txt
whitespace 0
 whitespace 1
  whitespace 2
[user@linux ~]$

[user@linux ~]$ grep ^w text.txt
whitespace 0
[user@linux ~]$
Run Code Online (Sandbox Code Playgroud)

有1个空格

[user@linux ~]$ grep '^[ ]w' text.txt
 whitespace 1
[user@linux ~]$
Run Code Online (Sandbox Code Playgroud)

有 2 个空格,但得到相同的输出

[user@linux ~]$ grep '^[  ]w' text.txt
 whitespace 1
[user@linux ~]$
Run Code Online (Sandbox Code Playgroud)

根据https://regex101.com/^[ ]{0,}是在行首查找空格的正确语法。然而,它在 Linux 上不能很好地与 GNU grep 配合使用。我收到错误Invalid regular expression

[user@linux ~]$ grep ^[ ]{0,}w text.txt
grep: Invalid …
Run Code Online (Sandbox Code Playgroud)

grep regular-expression

2
推荐指数
1
解决办法
1049
查看次数

标签 统计

grep ×1

regular-expression ×1