Parenthesis Check in Linux

In this blog, Sharing code to check whether Parenthesis or Brackets are correct in a string or not.Consider a String”[()]” should return “Parenthesis check Passed” while String “{()[(]))”should return “Parenthesis check Failed”. CODE EXPLANATION:1. Created two String OpenBracketStr and CloseBracketStr.2. Keep on Appending based on Character present in the String. If its a opening Bracket, it will be added to OpenBracketStr and as we have received Open Bracket type, I am adding a Expected Closed Bracket by myself in String called CloseBracketStr. In case I get 5 Opening bracket consecutively…

Minimum Index of all Characters in a String

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

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…

TWO ARRAYS ARE EQUAL OR NOT

In this blog, sharing code to check whether two arrays are equal or not . Below is the code explanation in 4 steps. 1. Check the length of each Array, if length is same , then we can compare , else exit.2. If length is same for both the Arrays, then arrange them in either ascending or descending order.3. Once arrays are sorted in an order, then compare element of each Array one by one and if elements are same, increment a dummy counter.4. If the value of counter is…

Check whether length of two array is same or not in Linux

In this blog, sharing code to check whether length of two arrays is same or not. arr=(4 3 2 1) ## First Array of 4 elementsarr_2=(4 3 1 10 1) ## Second Array of 5 elementslen_arr=${#arr[@]}len_arr_2=${#arr_2[@]}if [ $len_arr -eq $len_arr_2 ]thenecho “Length of both Array is same”fiif [ $len_arr -ne $len_arr_2 ]thenecho “Length of both Array is not same”fi