打印所有不包含标点符号的行

Ham*_*ava 8 grep regular-expression

我想要一个可以打印所有不包含标点符号的行的正则表达式模式:

输入 :

.This is line 1
This is ! line 2
This is line (3)
This is line 4
Run Code Online (Sandbox Code Playgroud)

输出:(应该是)

This is line 4
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的:

grep '[^[:punct:]]' file.txt
Run Code Online (Sandbox Code Playgroud)

但它显示了所有不是标点符号的字符。

Jos*_* R. 11

您将grep打印所有包含非标点字符的行。这与打印所有不包含标点字符的行不同。

对于后者,您需要-v开关(打印与模式不匹配的行):

grep -v '[[:punct:]]' file.txt
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因不想使用-v开关,则必须确保整行都包含非标点字符:

grep '^[^[:punct:]]\+$' file.txt
Run Code Online (Sandbox Code Playgroud)