Operating System - Linux
1824976 Members
3163 Online
109678 Solutions
New Discussion юеВ

search excat string in another string (grep "fails")

 
SOLVED
Go to solution
Billa-User
Regular Advisor

search excat string in another string (grep "fails")

hello,

i have an statement which i have to correct because it shows the wrong result.

i want to search an excat string in another string, command "grep" shows the wrong result:

example:

STRINGS="string1 string2 string3"
search_string="string"

incorrect:
if [ `echo "${STRINGS}" | grep "${search_string}" > /dev/null 2>/dev/null; \
echo $?` -gt 0 ]
then
echo "not found"
fi

my solution will be a "for statement":
exists="N"
for string in ${STRINGS}
do
if [ "${string}" = "${search_string}" ]
then
exists="J"
break
fi
done
echo $exists

is there any solution with one command. i will use in a shell script, also it possible to use perl for this but my experience in perl is very small ...

regards

11 REPLIES 11
mobidyc
Trusted Contributor
Solution

Re: search excat string in another string (grep "fails")

hello,

you can use le -w flag for grep:
#> STRINGS="string1 string2 string3"
#> search=string
#> echo "${STRINGS}" |grep -w "$search_string"
return nothing

#> search=string2
#> echo "${STRINGS}" |grep -w "$search_string"
string1 string2 string3

hope it'll help you

Regards,
Cerick Gaillard
Best regards, Cedrick Gaillard
Dennis Handly
Acclaimed Contributor

Re: search excat string in another string (grep "fails")

Using Cerick's suggestion, you can do:
#!/usr/bin/ksh
STRINGS="string1 string2 string3"
search_string="string"
echo "${STRINGS}" | grep -qw "${search_string}"
if [ $? -ne 0 ]; then
echo "not found"
fi
Billa-User
Regular Advisor

Re: search excat string in another string (grep "fails")

thank a lot for the very fast replies, i didn't know option "-w" before this day. it bring the exact result, great.
James R. Ferguson
Acclaimed Contributor

Re: search excat string in another string (grep "fails")

Hi:

Here's a Perl solution embedded in a shell:

# S="In here or there is his or her string"
# X="her"

# echo ${S} | perl -ne 'BEGIN{$X=shift or die};exit 0 if m{\b$X\b}i;exit 1' $(echo ${X})

# [ $? -eq 0 ] && echo "ok!" || echo "not found"
ok!

Notice that the match for the string ${X} in the string ${S} is a case-insensitive match that is bounded (\b) by non-word characters like the '-w' option of 'grep'.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: search excat string in another string (grep "fails")

Hi (again):

A quick addendum after much needed coffee. You certainly don't need the trailing $(echo ${X}) !!! The Perl snippet that emulates 'grep -qw' is simply:

# echo ${S} | perl -ne 'BEGIN{$X=shift or die};exit 0 if m{\b$X\b}i;exit 1' ${X}

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: search excat string in another string (grep "fails")

> [...] command "grep" shows the wrong result:

What (would you say) is the "right" result?
Would adding some spaces do what you want?

echo " ${STRINGS} " | grep " ${search_string} "
Dennis Handly
Acclaimed Contributor

Re: search excat string in another string (grep "fails")

>Steven: Would adding some spaces do what you want?

Unfortunately this doesn't work at beginning and ending of line or if delimited by punctuation. -w makes this all a no-brainer.
Steven Schweda
Honored Contributor

Re: search excat string in another string (grep "fails")

> Unfortunately this doesn't work at
> beginning and ending of line

Look again at where I put those spaces. It
sure seemed to work when I tried it. Did it
fail for you? (Demonstrate?)

> or if delimited by punctuation. -w makes
> this all a no-brainer.

I like whatever works, wherever it works. I
may still have an old grep somewhere which
doesn't do "-w", but I have spaces
everywhere.
Dennis Handly
Acclaimed Contributor

Re: search excat string in another string (grep "fails")

>Look again at where I put those spaces.

I was looking closer at the grep than the echo.
But my comment was more geared to files where there is whitespaces vs spaces, punctuation, etc.
Steven Schweda
Honored Contributor

Re: search excat string in another string (grep "fails")

> I was looking closer at the grep than the
> echo. [...]

And I was looking more closely at the problem
posed by the questioner.

As usual, depending on the details, there's
more than one way to solve a problem like
this. Many things are possible.
James R. Ferguson
Acclaimed Contributor

Re: search excat string in another string (grep "fails")

Hi:

> Steven: As usual, depending on the details, there's more than one way to solve a problem like this. Many things are possible.

Indeed that is one reason I suggested a Perl solution: TMTOWTDI

Regards!

...JRF...