这是我的示例文本。grep w
,grep ^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)