Position of a string

Can some one help me,to find the postion of the string in the files for all the files in the directory.

Thanks in advance...

This is beyond vague. Please give us one example.

ok,I want to find the postion of a string in all the files in the directory.

For Example:
The string below is in a file:
1234 345 678 the

I want to find the postion of the word/string "the".That will be 14.

Hope I am clear this time...

what if it is like this:

1234 345 678 the the

do you want to find the position of both?

or what about

1234 345 678 the their other
now "the" is repeated three times.

YEs, their might be multiple occurences of "the" in a file.And I want the starting postion of all.But the word("the") will occur only once in a line.
For example:

asdshdjsh the--11
sdahsdhaskj the --13
aksjdhashahskshjjsdfjd the--24

Thank you!!

Try this out.....

cat noname.txt
asdshdjsh the
sdahsdhaskj the
aksjdhashahskshjjsdfjd the

awk -v find=$var '{ printf ("%s %s %s %s %s %s\n", "The index of search value ", find, "in", $0, "is:-", index($0,find) ) }' noname.txt

NOTE: var is the parameter which you want to search.

o/p
The index of search value the in asdshdjsh the is:- 11
The index of search value the in sdahsdhaskj the is:- 13
The index of search value the in aksjdhashahskshjjsdfjd the is:- 24

Hi,

Not sure if I am allowed to write a query from a thread over one year ago , but it was most relevant to the help I needed.:slight_smile:

The last piece of code is very helpful, but what if one wants to search for the positions of multiple words on each line of file..e.g.

if it is like this:

1234345678upthehappytheir

I want to find index of both "the", which should be : 11 and then 19

Cheers! I hope someone would still read this old thread :eek:


## DESCRIPTION: print location of $substr in $string

string=$1
substr=$2

_index() 
{ 
    case $1 in 
        "") _INDEX=0;
            return 1
            ;;
        *"$2"*)
            idx=${1%%"$2"*};
            _INDEX=$(( ${#idx} + 1 ))
            ;;
        *)  _INDEX=0;
            return 1
            ;;
    esac
}

length=${#substr}
PAD=
PADCHAR=X
while [ ${#PAD} -lt $length ]
do
  PAD=$PAD$PADCHAR
done
while :
do
  case $string in
    *"$substr"*) _index "$string" "$substr" || break
                 echo "$_INDEX"
                 string=${string%%"$substr"*}$PAD${string#*"$substr"} 
                 ;;
    *) break ;;
  esac
done

echo 1234345678upthehappytheir | awk '{
START=1
while (match(substr($0,START),"the") && RSTART > 0 )
{
   print NR, RSTART+ START -1
   START += RSTART
}
}' | more
1 13
1 21