command line - finding a word in a string - Ask Ubuntu


i want find word abc in string (i want exact word abc, not words contain abc) following error:

echo "asjhdhahsjdajhsdabcasjdhas abc asdasdabc" | grep <abc> 

bash: syntax error near unexpected token `newline'

you want -o (show matched string only) , -w (match pattern whole word only)

$ echo "asjhdhahsjdajhsdabcasjdhas abc asdasdabc" | grep -ow abc abc 

thanks steeldriver explaining how can use < , > instead of -w indicate word boundaries. should \< , \> backslashes have quoted passed grep < symbols, since have special meaning shell. strong-quote expression this:

echo "asjhdhahsjdajhsdabcasjdhas abc asdasdabc" | grep -o '\<abc\>' 

or go crazy backslashes:

echo "asjhdhahsjdajhsdabcasjdhas abc asdasdabc" | grep -o \\\<abc\\\> 

Comments

Popular posts from this blog

Windows XP installation, no previous version of Windows NT - Super User

crash - Windows Rundll32 (child process of DllHost) is crashing. How can I even identify it? - Super User

networking - Slave steals IP meant for bonded interface - Ask Ubuntu