Grep regex filter

Hi,

How can I run the grep search in a script to only include compute, not bigcomputer in following search input?

"   properties = local compute"
"   properties = local bigcompute"

Thank you.

-j

--edit--

$ cat <<test | grep  -w 'compute'
" properties = local compute"
" properties = local bigcompute"
test

" properties = local compute"
1 Like

That's UUOC, could be simply <<test grep -w

1 Like

-w is an extension to POSIX grep and is not available on every platform. An alternative that should work in any POSIX grep would be:

grep -E '([^[:alnum:]_]|^)compute([^[:alnum:]_]|$)' file
1 Like

Thanks for all response, that are all great, but it is a little bit complicated, I forgot to mention that the search string needs to include "properties = ......" for only the line of "properties =" as other lines could also contain "computer" or "bigcomputer" as well.

-j

Then try this:

grep -E 'properties =.*[^[:alnum:]_]compute([^[:alnum:]_]|$)' file

If it does not need to be portable, on some platforms you can also try one of the following:

grep 'properties =.*[[:<:]]compute[[:>:]]' file
grep 'properties =.*\<compute\>' file
grep 'properties =.*\bcompute\b' file
1 Like

Terrific, thank you so much.