grep 表达式在读取conf 文件时行为怪异(unix/mac)

问题描述 投票:0回答:1

下面的 grep 表达式出了什么问题,感谢任何帮助。

$cat foo.txt

## not to be read
; this one too and next one also coz it has sapce
first_var= first_val
second_var=y
second_var=yes

现在当我在 Mac/Linux 上运行 grep 时

 grep "^[^#;].*[^[:space:]]\=[^[:space:]].*[^=]" foo.txt

我的输出低于

second_var=yes

正如我所期待的那样

second_var=y
second_var=yes

我这里出了什么问题?

regex shell unix scripting grep
1个回答
0
投票

=
之后的部分中,您期望有 2 个字符,但您只有一个
y

如果第二部分应以除

=

之外的任何字符结尾,则可以将第二部分设为可选
grep "^[^#;].*[^[:space:]]=[^[:space:]]\(.*[^=]\)\?" foo.txt  

输出

second_var=y
second_var=yes
© www.soinside.com 2019 - 2024. All rights reserved.