Linux -> 终端命令 -> Grep -> 与符号相关的表达式/异常

Linux -> Terminal command -> Grep -> Expressions / Exceptions related to symbol

简单地说:我的命令有问题,它应该打印出包含这两个表达式中的任何一个的行:“king”"king's son"。这是我到目前为止的进展:

grep -w "king's son\|king" frog.txt

它确实有效,但它包括 "king's"不应该 发生。

添加 -v grep "king's" 不起作用,因为它也会删除 "king's son"

我正在使用安装在 Virtual Box Machine 上的 Ubuntu 32 位系统。

grep -E "([[:space:]]|^)king('s son)?([[:space:]]|$)" frog.txt

例如,如果 frog.txt 包含

kingb    # no match
king's   # no match
king-bee # no match 
breaking # no match
king's hello # no match
king's sonth # no match

king     # match
a king bee  # match
king    bee # match (with a TAB)
king's son  # match

然后上面的命令returns

king     # match
a king bee  # match
king    bee # match (with a TAB)
king's son  # match
grep -w "king's son\|king$" frog.txt

-w 不会有太大帮助,因为 kingking's 中被认为是一个单词,因为 ' 是一个非单词字符。

使用:

grep -E "([[:space:]]|^)king('s son)?([[:space:]]|$)" frog.txt

或者如果您的 grep 有可用的 PCRE 选项,则使用 lookarounds

grep -P "(?<=[[:space:]]|^)king('s son)?(?=[[:space:]]|$)" frog.txt

以下行可能适合您的情况。

grep -w "king's son\|king$\|king\ " frog.txt

结果是:

king's son   #match
king         #match
king's hello #not match

应该这样做:

grep -E "(^|[ \t])(king|king's son)([ \t]|$)" frog.txt

它使用组 (^|[ \t])([ \t]|$) 来匹配单词分隔符或行的 beginning/end。