简体   繁体   中英

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

Simply: I have problem with command which should print me lines containing any of those two expressions: " king" , "king's son" . This is where I got so far:

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

It does work but it include "king's" which should not happen.

Adding -v grep "king's" does not work as it deletes "king's son" also.

I am using Ubuntu 32 bit system installed on Virtual Box Machine.

-w won't help much because king is considered a word in king's as ' is a non-word character.

Use:

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

Or using lookarounds if your grep has PCRE option available:

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

For example, if frog.txt contains

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

then the above command returns

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

This oughta do it:

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

It uses the groups (^|[ \\t]) and ([ \\t]|$) to match word separators or the beginning/end of lines.

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

the following line maybe work for your situation.

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

the result is :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM