How do I remove the shortest & longest trailing patterns of a substring in bash shell script?



  • Remove shortest leading substring that matches pattern.

    Usage is ${name%pat}

    Remove longest leading substring that matches pattern.

    Usage is ${name%%pat}

    declare -A MyBucketList MyKtbList MyList
    MyBucketList=([1]="The rain in Spain stays mainly on the plain" [2]="Go to Mars" [3]="Climb Mount Everest")
    echo ${MyBucketList[1]%a*n}
    

    The main line to observe is line 3. Running the script gives the following results. This is the shortest trailing matching pattern.

    $ ./associative_array.sh
    The rain in Spain stays maily on the pl
    

    Changing line 3 to echo ${MyBucketList[1]%%a*n} and running the script again. The longest trailing matching pattern, it goes back to ain in the word rain and removes the rest.

    $ ./associative_array.sh
    The r
    

Log in to reply
 

© Lightnetics 2024