[英]Linux -> Terminal command -> Grep -> Expressions / Exceptions related to symbol
简而言之:我的命令有问题,该命令应该向我显示包含以下两个表达式之一的行:“ king” , “ king's son” 。 这是我到目前为止的去处:
grep -w "king's son\|king" frog.txt
它确实有效,但是它包含不应发生的“国王” 。
添加-v grep "king's"
不起作用,因为它也会删除“ king's son” 。
我正在使用Virtual Box Machine上安装的Ubuntu 32位系统。
-w
也不会有多大效果,因为king
是在考虑一个字king's
为'
是一个非字字符。
采用:
grep -E "([[:space:]]|^)king('s son)?([[:space:]]|$)" frog.txt
或者,如果您的grep
具有可用的PCRE选项,则使用环视方法 :
grep -P "(?<=[[:space:]]|^)king('s son)?(?=[[:space:]]|$)" frog.txt
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
然后上面的命令返回
king # match
a king bee # match
king bee # match (with a TAB)
king's son # match
这应该做到:
grep -E "(^|[ \t])(king|king's son)([ \t]|$)" frog.txt
它使用组(^|[ \\t])
和([ \\t]|$)
来匹配单词分隔符或行的开头/结尾。
grep -w "king's son\|king$" frog.txt
以下几行可能适合您的情况。
grep -w "king's son\|king$\|king\ " frog.txt
结果是:
king's son #match
king #match
king's hello #not match
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.