Operating System - HP-UX
1832927 Members
3131 Online
110048 Solutions
New Discussion

Re: how do i find a word in a string in ksh

 
SOLVED
Go to solution
Sachin_29
Advisor

how do i find a word in a string in ksh

I want to look for a specific word in a string in a ksh.
there is a PAth A/b/c/d
I have to check whether $string or $string1 or $string2 is in the Path?
Any ideas
thanks,
3 REPLIES 3
Rodney Hills
Honored Contributor
Solution

Re: how do i find a word in a string in ksh

Try the following-

x="A/b/c/d"
if [[ "$x" = *$string1* ]] ; then echo yes ; else echo no ; fi

HTH

-- Rod Hills
There be dragons...
Mel Burslan
Honored Contributor

Re: how do i find a word in a string in ksh

Detailed and easy to understand solution to it, could be:


DIR_PATH=/home/dev/projects/finance/cash #as an example
STRING1=user1 #as an example
STRING2=project #as an example
STRING3=general #as an example

foundflag=0
echo $PATH | grep -q $STRING1
r=`echo $?`
if [ $r -eq 0 ]
then
foundflag=1
fi

echo $PATH | grep -q $STRING2
r=`echo $?`
if [ $r -eq 0 ]
then
foundflag=1
fi

echo $PATH | grep -q $STRING3
r=`echo $?`
if [ $r -eq 0 ]
then
foundflag=1
fi

if [ $foundflag -eq 0 ]
then
echo "String NOT FOUND"
else
echo "String found"
fi


________________________________
UNIX because I majored in cryptology...
Sachin_29
Advisor

Re: how do i find a word in a string in ksh

Thanks all!!