1846112 Members
4619 Online
110253 Solutions
New Discussion

String checking

 
SOLVED
Go to solution
dude70_1
Advisor

String checking

Hi Guys,

In my shell script I have 2 strings:

s1="test string"
s2=" this is a test string but it is longer"

I have to find whether s2 contains s1 if true I have to some operation any command help will be helpful.

Thanks.
3 REPLIES 3
James A. Donovan
Honored Contributor

Re: String checking

echo $s2|grep "$s1"
Remember, wherever you go, there you are...
Karthik S S
Honored Contributor
Solution

Re: String checking

Edit your script to look like this,

s1="test string"
s2=" this is a test string but it is longer"
echo $s2 |grep "\<$s1\>"
if [ $? -eq 0 ]
then
echo s2 contains s1
fi

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Michael Schulte zur Sur
Honored Contributor

Re: String checking

Hi,

expr "${s2}" : ".*\(${s1}\).*"

returns s1 if it is included in s2.

have fun,

Michael