我有一个文本文件:
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
选项,但是我的字符串必须整行。
在回答另一个问题时,我引用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 ×2