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

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User