Unix Blog

 
 
 
echo Enter main string:
read main
l1=\`echo $main | wc -c\`
l1=\`expr $l1 - 1\`
echo Enter sub string:
read sub
l2=\`echo $sub | wc -c\`
l2=\`expr $l2 - 1\`
n=1
m=1
pos=0
while [ $n -le $l1 ]
do
a=\`echo $main | cut -c $n\`
b=\`echo $sub | cut -c $m\`
if [ $a = $b ]
then
n=\`expr $n + 1\`
m=\`expr $m + 1\`
pos=\`expr $n - $l2\`
r=\`expr $m - 1\`
if [ $r -eq $l2 ]
then
break
fi
else
pos=0
m=1
n=\`expr $n + 1\`
fi
done
echo Position of sub string in main string is $pos

    Related Post:
  1. Shell script to display the name of those files (in the given directory) which are having multiple links

  2. Display your login name

  3. Shell script to change extension of an existing file

  4. Write a shell script, which will receive any number of filenames as arguments .The shell script should check whether such files already exist.

  5. Shell Script to make a menu driven calculator using case

  6. Shell script to check whether inputted directory is in current directory or not

  7. Write a shell script, which reports names and sizes of all files in a directory whose size is exceeding 1000 bytes

  8. Write a shell script to find the sum, the average and the product of the four integers entered

  9. Remove entire directory structure

  10. Write a shell scripts that finds total no. of users and finds out how many of them are currently loggedin

  11. Shell script to print first 5 odd numbers

  12. Shell script to perform operations like compare string, concatenate, find length, occurrence of word in a string and reverse a string

  13. Write a shell script to convert a file to lower case or upper case as specified by user.

  14. Write a shell script that accepts a string from the terminal and echo suitable message if it doesn\’t have at least ten characters

  15. Write a shell script, which will receive any number of filenames as arguments .The shell script should check whether such files already exist.

  16. Find all lines in a file with words longer than 4 characters, assuming that words are separated by spaces except at the begining or end of line

  17. Find the lines containing a number in a file

  18. Find all lines in a file with words longer than 4 characters, assuming that words are separated by spaces except at the beginning or end of line

  19. Shell script to take LOGNAME or the UID an an input and find how many terminals has this user logged in

  20. Prepare a menu driven calculator

 
 
Didn't find what you were looking for? Find more on Write a shell program to find the position of substring in given string