ksh cut out words from string

Hi,

I have:

export string1=qwerWhatever
export string2=qwerWhatever1
export currdir=`pwd`

echo $currdir gives back:

/dir/dir/Whatever1

I want to take first 4 letters from string1 (in this case: qwer), compare it to string2 (in this case qwerWhatever1) and if string2 has in it string1, remove string1 from string2 and put it to string3, so string3 would be: Whatever1. And after this I want to compare if $currdir has $string3.

To take first four letters I can do:

echo $string1 | awk '{print substr($1,1,4) }'

but I am not sure what to do else.

Maybe somebody can help me how this can be established.

if your characters are same. what i meant was if the first four characters are always same then you can do something like this:-

lets just say the first four characters are =abcd
export string1=abcdWhatever
export string2=abcdWhatever1
export currdir=`pwd`
echo $currdir /dir/dir/Whatever1

if [[ `grep "abcd" string1` ]]
then 
   if [[ `grep "abcd" string2` ]]
   then
    cat string2 | grep -v "abcd" string2 > string3
   fi
fi

if [[ $curdir == string3 ]]
then 
echo "string 3 is equal to current directory"
fi

with bash (version above 3)

$ cat match.sh 
#!/usr/local/bin/bash
 
 
string1=qwerWhatever
string2=qwerWhatever1
currdir=/dir/dir/Whatever1
 
 
s1f4=$(echo $string1 | cut -c1-4)
if [[ "$string2" =~ $s1f4 ]]; then
  echo $s1f4 found in string2
  strng3=$(echo $string2 | sed "s/\(.*\)$s1f4\(.*\)/\1\2/")
  if [[ "$currdir" =~ "$string3" ]]; then
        echo Current dir has string3
  fi
else
  echo Intial match failed
fi
$ 
$ 
$ 
$ 
$ ./match.sh 
qwer found in string2
Current dir has string3
$ 

Thank you, that's exactly what I want, but what this in bash means?

 =~ 

I am trying to search analog for KSH for it, because it reports that "=~" unexpected.

That is bash's built-in comparison operator for regular expressions.
Introduced in bash version 3.
that will work only in bash shell. if you want to stick with the ksh, you probably need to use external commands grep/sed.

have a look here for more details.

Another Solution.........

#!/usr/bin/ksh

string1=qwerWhatever
string2=qwerWhatever1
currdir=`pwd`

echo "string1 ==> $string1"
echo "string2 ==> $string2"
echo "currdir ==> $currdir"

string3=$(nawk -v VAR1=`echo $string1 | cut -c1-4` -v VAR2=$string2 'BEGIN {if (VAR2~VAR1) {gsub(VAR1,"",VAR2);print VAR2} else {print VAR1 " not found in string2"; system("exit")}exit;}' )
echo "string3 ==> $string3"

nawk -v VAR3=$string3 -v CURDIR=$currdir 'BEGIN {if (CURDIR ~ VAR3) print "currdir has" VAR3 ;else print "currdir does not have " VAR3 ; exit;}'
1 Like

no need of cut

string1=qwerWhatever
s1f4=${string1:0:4}
echo $s1f4
qwer

no need of sed

string2=qwerWhatever1
string3=${string2//$s1f4/}
echo $string3
Whatever1
1 Like

daPeach,
Which shell r u using?

>cat 1
#!/usr/bin/ksh

string1=qwerWhatever
s1f4=${string1:0:4}
echo $s1f4

>
>1
1[4]: s1f4=${string1:0:4}: bad substitution

I'm on ksh93, and bash. It also works with Bourne shell.