Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding string within a string( VERY URGENT) 1

Status
Not open for further replies.

encourage

Programmer
Apr 16, 2002
5
IN
consider the following example:
string1=dogs
string2 = animalsdogscatsdogs

now i need to find the starting position of string1 within string2 specified from a given position.

if d = [string1,string2]
then the value of d will be 8 as dogs starts from the 8th position in string2

now if d = [string1,string2,5]
then the value of d will be 8 since the starting position is from 5th position but still dogs start from position 8 in string2.

now if d = [string1,string2,10]
then the value of d will be 16 as the matching starts from position 10.

i need to atain the same functionality as described above usind awk or nawk or C SHELL.

I HAVE ATTAINED THE ABOVE FUNCTIONALITY USING NAWK BUT UNABLE TO GIVE THE STARTING POSITION SUCH AS 5 OR 10.

set tofind = $argv[1]
set string = $argv[2]
nawk -v TF=${tofind} -v STR=${string} 'BEGIN { print index(STR, TF) }'


PLEASE CAN ANYONE HELP ME OUT.
 


#--------------------- dog.awk ----------------------
BEGIN {
if ( start == "" )
start=1;
}

{
str=substr($0, start);
pos=match(str, sstr);
print("%d\n", (pos==0) ? pos : pos+start-1);
}

#-------------------------------------------------------

$ echo "animalsdogscatsdogs" | awk -v start=10 -v sstr=dog -f dog.awk

16

$ echo "animalsdogscatsdogs" | awk -v sstr=dog -f dog.awk

8
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top