In this blog, Sharing code to find Minimum Index value of all Characters present in a String. Consider a String “allabouttechnologies”.So, mininum Index for l is 1 not 2 and for a, its 0 not 3. str=”alllabouttechnologies”declare -A dict1 ##declare a dictionaryunset dict1 ##resetting dictionarynewstr=” ## creating Blank Stringn=${#str}i=0while [[ $i -lt $n ]]doChar=${str:$i:1}echo ${newstr}CharCntInNewStr=echo ${newstr} | grep ${Char} | wc -l ##If Character is not present, inserting into dictionary Index and Character itselfif [[ $CharCntInNewStr -eq “0” ]]thendict1[${i}]=${Char}echo ${dict1[${i}]}newstr=${newstr}${str:$i:1}fii=$(( $i + 1))donefor key in “${!dict1[@]}”; doecho “$key ${dict1[$key]}”done
Day: December 26, 2022
Get all distinct Character in a string
In this blog, Sharing code to get only unique Characters present in a string. Please do not get confused with my Previous Blog . Its a different topic all together. Consider a String “allabouttechnologies“, so the distinct or unique Characters of this String are “alboutechngis“. str=”alllabouttechnologies” ##Sample Stringnewstr=” ##Blank new Stringn=${#str} ## Length of a Stringi=0while [[ $i -le $n ]] ##looping Character by CharacterdoChar=${str:$i:1}CharCntInNewStr=echo ${newstr}| grep ${Char} | wc -l ##Checking if Character already exists in new Stringif [[ $CharCntInNewStr -eq “0” ]]thennewstr=${newstr}${str:$i:1} ## appending Character if it is…
Remove all repeated Characters in a String
In this blog, sharing the code to remove consecutively repeated characters in a String. Consider a String “allabouttechnologies” . Here, l and t are repeated Characters. So, after applying logic, it will be like “alaboutechnologies“. str=”alllabouttechnologies” ##Sample Stringnewstr=” ##new Blank Stringn=${#str} ##length of a Sample StringPrevChar=${str:0:1} ##First Character of a Stringi=1while [[ $i -le $n ]] ##Looping Character by CharacterdoNextChar=${str:$i:1}if [[ $PrevChar == $NextChar ]] ##if two consecutive Character matches, it will remove or ignore the matched one and keep repeated Character oncethenecho “Ignore this Character: “$NextCharelsenewstr=${newstr}${PrevChar} ##merging only non-repeated…