相关疑难解决方法(0)

使用 grep 匹配精确的字符串

我有一个文本文件:

deiauk 1611516 afsdf 765
minkra 18415151 asdsf 4152
linkra sfsfdsfs sdfss 4555
deiauk1 sdfsfdsfs 1561 51
deiauk2 115151 5454 4
deiauk 1611516 afsdf ddfgfgd
luktol1 4545 4 9
luktol 1
Run Code Online (Sandbox Code Playgroud)

我想完全匹配deiauk。当我这样做时:

grep "deiauk" file.txt
Run Code Online (Sandbox Code Playgroud)

我得到这个结果:

deiauk 1611516 afsdf 765
deiauk1 sdfsfdsfs 1561 51
deiauk2 115151 5454 4
Run Code Online (Sandbox Code Playgroud)

但我只需要这个:

deiauk 1611516 afsdf 765
deiauk 1611516 afsdf ddfgfgd
Run Code Online (Sandbox Code Playgroud)

我知道有一个-w选项,但是我的字符串必须整行。

grep

107
推荐指数
4
解决办法
55万
查看次数

为什么 grep -x 暗示将模式括起来?

回答另一个问题时,我引用man grep了关于-x

-x, --line-regexp
       Select only those matches that exactly match the whole line.
       For a regular expression pattern, this is like parenthesizing
       the pattern and then surrounding it with ^ and $.
Run Code Online (Sandbox Code Playgroud)

我已经知道这-x就像“用^和”包围[正则表达式] $,但为什么它还暗示“将模式括起来”?

我想不出为什么括号是必要的,甚至无法改变任何东西。如果整个模式都用括号括起来,你就不能用捕获组在内部引用它。grep抱怨尾随反斜杠,因此附加的)也没有奇怪的行为(我也不希望这符合选项的精神)。

为什么man grep特别提到parenthesizing the pattern-x

grep

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

标签 统计

grep ×2