📌  相关文章
📜  Shell 程序在给定字符串中查找子字符串的位置

📅  最后修改于: 2022-05-13 01:57:29.908000             🧑  作者: Mango

Shell 程序在给定字符串中查找子字符串的位置

一个字符串由许多子字符串组成,或者可以说,如果我们从开头或结尾删除一个或多个字符,则剩余的字符串称为子字符串。本文即将编写一个 shell 程序,它会告诉子字符串在给定字符串的位置(索引)。让我们举一个例子。

例子:

Given ( String ): "geeks for geeks is the best platform for computer science geek"
Input ( Substring ): "computer"
Output ( Position ): 42 ( string index starting from 1 ) ( first index, from where substring start )

我们将在这里主要讨论两种方法,第一种是蛮力方法,第二种是使用 bash 命令。

方法一:

要编写脚本代码,请按照以下步骤操作



步骤1:做两个选择输入主字符串或继续默认。

第二步:根据用户的选择,使用read命令获取user get 字符串和substring。

第 3 步:现在,运行一个循环以获取给定字符串的所有字符,并将它们与子字符串的第一个字符进行比较。

第 4 步:再次运行循环以检查从匹配字符到匹配所有其他字符以查找子字符串。

外壳脚本代码:

# script to get the substring position in given string

# let give a string 
str="geeks for geeks is the best platform for computer science geeks"

# now ask user to give the new string or use the given default string
echo "Hello there, do you wanna give new string or use default"
echo 
echo "Enter 1 for new string"
echo "Enter 0 for continue"

# now read the choice form user
read choice
echo

# make the condition to check the choice and perform action according to that
if [[ choice == 1 ]]
then 
    # now ask reader to give the main string
    echo "Please, Enter the main string"
    
    # now read the string 
    read str
    echo
fi

# print a massage
echo "Let's continue to get the index of the substring....."
echo

# make a loop to get the substring values from the user 
while [[ 1 ]]
do 
    # print the statement
    echo "Enter a substring to get the position of that string OR Enter -1 to get exit"
    
    # now read the substr
    read substr
    
    # make a condition to check the value of substr
    if [[ $substr != -1 ]]
    then 
        # # 1st approach code to get the substring position from given string ( 1st approach )
        # # This approach is comparison on char by char 
        # ************************************************************************
        
        # length of the given string
        lenGS=${#str}
        #length of the substr
        lenSS=${#substr}
        
        # check the condition where string length is less than substring length
        if [[ $lenGS -lt $lenSS ]] 
        then 
            echo "Sorry, Your substring exceed main string, Please Enter another"
            continue
        fi
        
        # variable to store position 
        pos=-1
        # variable to check
        found=0
        
        # run three native loop ( brute force appraoch ) 
        for (( i=0;i

脚本的执行:

Command: bash script.sh
Hello there, do you wanna give new string or use default

Enter 1 for new string
Enter 0 for continue
0

Let's continue to get the index of the substring.....

Enter a substring to get the position of that string OR Enter -1 to get exit
geeks
Your substring geeks is found at the index 1

Enter a substring to get the position of that string OR Enter -1 to get exit
best
Your substring best is found at the index 1

Enter a substring to get the position of that string OR Enter -1 to get exit
web 
Sorry, Your substring web is not found in main string

Enter a substring to get the position of that string OR Enter -1 to get exit
-1
okay! Closed

输出截图:

脚本 1



方法二:

要编写脚本代码,请按照以下步骤操作

步骤1:做两个选择输入主字符串或继续默认。

第二步:根据用户的选择使用read命令获取user的字符串。

步骤3:使用read命令从用户处获取子字符串。

第 4 步:现在运行一个循环并使用脚本中的 substring函数获取给定子字符串长度的主字符串的所有子字符串。

步骤5:一一检查所有子串,当找到等于给定子串的子串时停止。

外壳脚本代码:

# script to get the substring position in given string

# let give a string 
str="geeks for geeks is the best platform for computer science geeks"

# now ask user to give the new string or use the given default string
echo "Hello there, do you wanna give new string or use default"
echo 
echo "Enter 1 for new string"
echo "Enter 0 for continue"

# now read the choice form user
read choice
echo

# make the condition to check the choice and perform action according to that
if [[ choice == 1 ]]
then 
    # now ask reader to give the main string
    echo "Please, Enter the main string"
    
    # now read the string 
    read str
    echo
fi

# print a massage
echo "Let's continue to get the index of the substring....."
echo

# make a loop to get the substring values from the user 
while [[ 1 ]]
do 
    # print the statement
    echo "Enter a substring to get the position of that string OR Enter -1 to get exit"
    
    # now read the substr
    read substr
    
    # make a condition to check the value of substr
    if [[ $substr != -1 ]]
    then 
        # # 2nd approach code to get the substring position from given string ( 2nd approach )
        # # This approach is comparison on string by string using bash string function 
        # ************************************************************************
        
        pos=-1
        
        # length of the given string
        lenGS=${#str}
        #length of the substr
        lenSS=${#substr}
        
        # check the condition where string length is less than substring length
        if [[ $lenGS -lt $lenSS ]] 
        then 
            echo "Sorry, Your substring exceed main string, Please Enter another"
            continue
        fi
        
        # get the limit of the loop
        limit=`expr $lenGS - $lenSS + 1`
        
        # variable to check
        found=0
        
        # run a loop to check the all substring 
        for (( i=0; i

脚本的执行:

Command: bash script.sh
Hello there, do you wanna give new string or use default

Enter 1 for new string
Enter 0 for continue
0

Let's continue to get the index of the substring.....

Enter a substring to get the position of that string OR Enter -1 to get exit
computer
So computer substring position in main string is : 42

Enter a substring to get the position of that string OR Enter -1 to get exit
best
So best substring position in main string is : 24

Enter a substring to get the position of that string OR Enter -1 to get exit
geeksgeeks
Sorry, Your substring geeksgeeks is not found in main string

Enter a substring to get the position of that string OR Enter -1 to get exit
-1
okay! Closed

输出截图:

脚本 2脚本 2