grep -w uses punctuations and whitespaces as delimiters.
How can I set grep to only use whitespaces as a delimiter for a word?
grep -w uses punctuations and whitespaces as delimiters.
How can I set grep to only use whitespaces as a delimiter for a word?
If you want to match just spaces:
grep -w foo
is the same asgrep " foo "
. If you also want to match line endings or tabs you can start doing things like:grep '\(^\| \)foo\($\| \)'
, but you're probably better off withperl -ne 'print if /\sfoo\s/'
The --word-regexp flag is useful, but limited. The grep man page says:
If you want to use custom field separators, awk may be a better fit for you. Or you could just write an extended regular expression with egrep or
grep --extended-regexp
that gives you more control over your search pattern.You cannot change the way
grep -w
works. However, you can replace punctuations with, say,X
character usingtr
orsed
and then usegrep -w
, that will do the trick.Use
tr
to replace spaces with new lines. Thengrep
your string. The contiguous string I needed was being split up withgrep -w
because it has colons in it. Furthermore, I only knew the first part, and the second part was the unknown data I needed to pull. Therefore, the following helped me.