文件:

cat test.txt

100 ***

10 ** // target

格雷普:
grep -E '\*{2}' test.txt

100 ***

10 **

不是用{m}来做精确的匹配数吗?
如何得到精确发生2次的*

最佳答案

你可以用

grep -E '(^|[^*])\*{2}($|[^*])' test.txt

online grep demo
test="100 ***
10 ** // target"
grep -E '(^|[^*])\*{2}($|[^*])' <<< "$test"
# => 10 ** // target

细节
(^|[^*])-字符串或任何字符的开头,但*
\*{2}-两个星号
($|[^*])-字符串或任何字符的结尾,但*
与空白边界的变化
如果您只需要在空格之间匹配一个“单词”,请使用
grep -E '(^|[[:space:]])\*{2}($|[[:space:]])' test.txt

09-09 21:03
查看更多