skip to Main Content

I want to use perl to get the index of from any given string and substring in bash function.

Here is the example for getting the indexOf value from perl script:

https://www.geeksforgeeks.org/perl-index-function/

#!/usr/bin/perl
 
# String from which Substring 
# is to be searched 
$string = "Geeks are the best";
 
# Using index() to search for substring
$index = index ($string, 'the');
 
# Printing the position of the substring
print "Position of 'the' in the string: $indexn";

Output:

Position of 'the' in the string: 10

Here is the test.sh:

#!/bin/bash

bash_function_get_index_from_perl_script() {
    local index="-1"
    
    # Here is the dummy code 
    # as I don't know how to convert
    # the perl script to bash command lines
    # and get the result from perl script
    
    index="
    
        #!/usr/bin/perl
         
        # String from which Substring 
        # is to be searched 
        $string = "Geeks are the best";
         
        # Using index() to search for substring
        $index = index ($string, 'the');
         
        # Printing the position of the substring
        print "Position of 'the' in the string: $indexn";
    
    "
    
    printf "%s" "$index"
}

result="$(bash_function_get_index_from_perl_script)"

echo "result is: $result"

Here is the expected output:

result is: 10

How to implement the "bash_function_get_index_from_perl_script"?

Update:

Here is the bash function for testing:

#!/bin/bash

bash_function_get_index() {
    local string="$1"
    local search_string="$2"
    
    before=${string%${search_string}*}
    index=${#before}
    
    printf "%s" "$index"
}

echo "test 1: $(bash_function_get_index "hello world" "wo")"
echo "test 2: $(bash_function_get_index "hello world" "wox")"

Here is the output:

test 1: 6
test 2: 11

The result of "test 2" is wrong, it should be:

test 2: -1

I want to implement the same "indexOf" function from Java:

https://www.w3schools.com/java/ref_string_indexof.asp

index_of() {
    local string="$1"
    local search_string="$2"
    local fromIndex="$3"
    
    local index=""
    
    # Get the real index here, the result shoule be the same as the Java's "indexOf"
        
    printf "%s" "$index"
}

2

Answers


  1. There are many ways. For example, you can send the script to the standard input of perl:

    #!/bin/bash
    
    bash_function_get_index_from_perl_script() {
        index=$(perl <<- '_PERL_'
        $string = 'Geeks are the best';
        $index = index $string, 'the';
        print "Position of 'the' in the string: $indexn";
        _PERL_
        )
        printf "%s" "$index"
    }
    
    result=$(bash_function_get_index_from_perl_script)
    
    echo "result is: $result"
    

    But you don’t need Perl for that, you can find the position using parameter expansion in bash itself:

    #!/bin/bash
    
    bash_function_get_index() {
        string=$1
        search=$2
        [[ $string = *"$search"* ]] || { printf %s -1 ; return ; }
        before=${string%"$search"*}
        index=${#before}
        printf %s "$index"
    }
    
    for s in the thex ; do
        result=$(bash_function_get_index 'Geeks are the best' "$s")
        echo "Position of '$s' in the string: $result"
    done
    
    Login or Signup to reply.
  2. Index Of substring in string in

    Try this:

    string="Geeks are the best"
    sub=the
    lhs=${string%"$sub"*}
    echo ${#lhs}
    10
    

    As a function

    with -v option to assign a variable instead of echoing. In order to avoid useless forks:

    indexOf() {
        if [[ $1 == -v ]]; then
            local -n _iO_result="$2"
            shift 2
        else
            local _iO_result
        fi
        local _iO_string="$2" _iO_substr="$1" _iO_lhs
        _iO_lhs=${_iO_string%"$_iO_substr"*}
        _iO_result=${#_iO_lhs}
        ((_iO_result == ${#_iO_string} )) && _iO_result=-1
        case ${_iO_result@A} in _iO_result=* ) echo "$_iO_result";;esac
    }
    

    With an exeption: if substring not found, then answer is -1.

    Tests and syntax

    Then

    indexOf the "Geeks are the best"
    10
    
    indexOf -v var the "Geeks are the best"
    echo $var
    10
    
    indexOf bad "Hello good world!"
    -1
    
    indexOf '*' 'abc*efg???x'
    3
    

    Then for avoiding forks var=$(function...), use -v var option!!

    tcnt=1;for tests in  $'wothello world' $'woxthello world' $'*tabc*efg???x'; do
        tststr=${tests%%$'t'*} string=${tests#*$'t'}
        indexOf -v idx "$tststr" "$string"
        printf 'Test %2d: result: %3d. string "%s" substring: "%s"n' 
            $((tcnt++)) "$idx" "$string" "$tststr"
    done
    

    should produce:

    Test  1: result:   6. string "hello world" substring: "wo"
    Test  2: result:  -1. string "hello world" substring: "wox"
    Test  3: result:   3. string "abc*efg???x" substring: "*"
    

    Why do I insist about avoiding forks

    Just try to run 1’000 time the job:

    time for ((i=1000;i--;)){ idx=$(indexOf 'wo' 'Hello world') ;};echo $idx
    
    real    0m0.948s
    user    0m0.608s
    sys     0m0.337s
    6
    
    time for ((i=1000;i--;)){ indexOf -v idx 'wo' 'Hello world' ;};echo $idx
    
    real    0m0.030s
    user    0m0.030s
    sys     0m0.000s
    6
    

    From approx 1 seconds to approx 3 100th of second!!

    No comment!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search