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
Post a Comment