For loop to break apart word

notimes=5
word=excellency

the word excellency contains 10 letters. 10 letters divided by 2 = 5. which means, 5 two-groups of letters are in the word excellency.

i need to perform a function on each group of letters. but the only thing i can think of is the following, which i just know isn't efficient:

while [ $notimes -le 5 ] ; 
do
lum1=$(echo $word | cut -c1-2)
lum2=$(echo $word | cut -c3-4)
lum3=$(echo $word | cut -c5-6)
lum4=$(echo $word | cut -c7-8)
lum5=$(echo $word | cut -c9-10)
mycommand lum1 lum2 ......
 
done

in the above scenario, we know the word (excellency). but in the scenario this script will work on, the words will be unknown. but the number of characters in whatever the word is will always be divided by 2.

in the above while loop scenario, i knew the number of characters in the word, because i knew the word, so i hardcoded the "cut" command to break it apart. but that wont be case when this script is put to work.

how can i automate the while loop? awk?

$ set $(echo $word | fold -2)
$ echo $1 $2 $3 $4 $5
ex ce ll en cy
2 Likes
word=excellency
i=0
len=${#word}
while ((i<${#word}))
do
 echo "${word:$i:2}"
 ((i+=2))
done

or a tweak to anbu23's solution:

# echo $1 $2 $3 $4 $5
echo "$@"
1 Like

i have a scenario which is related to this.

so running the while loop provided by elixir,

i got this:

code1="ex ce ll en cy"
code2="ex be uu kk nn"

i need to compare each 2-group word in code1 to code2, or vice versa.

meaning, i need to compare

ex to ex
ce to be
ll to uu
en to kk
cy to nn

i tried

for twogroup in `echo $code1`
do
      for code2word in `echo $code2`
       do
             .....here is where i'm lost
       done
done

Array element comparison:

#!/bin/bash

i=0
declare -a code1=(ex ce ll en cy)
declare -a code2=(ex be uu kk nn)

while [ $i -lt ${#code1[@]} ]
do
        [[ "${code1[$i]}" = "${code2[$i]}" ]] && echo "${code1[$i]} - ${code2[$i]} Match" || echo "${code1[$i]} - ${code2[$i]} Unmatch"
        i=$(( i + 1 ))
done
1 Like

Here is another example of how to do this that uses a function to split strings into arrays and then compares the array elements. This script uses several extensions to the standards, but works with recent versions of both ksh and bash:

#!/bin/ksh
# Usage: tryme string1 string2 [stringX stringX+1]...
# DESCRIPTION:
#       The tryme utility will demonstrate the use of the split2() shell
#       function by calling it twice for each pair of operands supplied.  It
#       then reports whether each pair of characters in the corresponding
#       strings match.
split2() {
# Usage: split2 arrray_name string
# DESCRIPTION:
#       The split2 function will split string into 2 character pairs and assign
#       those pairs to elements of the array specified by array_name with
#       subscripts starting with 1.  If string consists of an odd number of
#       characters, the last element assigned to the array will only contain
#       one character.
        s2_tmp2=1
        for((s2_tmp1 = 0; s2_tmp1 < ${#2}; s2_tmp1 += 2))
        do
                eval $1[$s2_tmp2]=${2:$s2_tmp1:2}
                s2_tmp2=$((s2_tmp2 + 1))
        done
}
while [ $# -ge 2 ]
do
        printf "Processing %s and %s\n" "$1" "$2"
        split2 v1 "$1"
        split2 v2 "$2"
        if [ ${#v1[@]} -ge ${#v2[@]} ]
        then    m=${#v1[@]}
        else    m=${#v2[@]}
        fi
        for((i = 1; i <= m; i++))
        do      printf "v1[%d]=%s, v2[%d]=%s\n" $i "${v1[$i]}" $i "${v2[$i]}"
                if [[ "X${v1[$i]}" == "X${v2[$i]}" ]]
                then    printf "v1[%d] is the same as v2[%d]\n" $i $i
                else    printf "v1[%d] is not the same as v2[%d]\n" $i $i
                fi
        done
        shift 2
done

An example using this script is:

tryme excellency exbeuukknn 12345 abc

which produces the output:

Processing excellency and exbeuukknn
v1[1]=ex, v2[1]=ex
v1[1] is the same as v2[1]
v1[2]=ce, v2[2]=be
v1[2] is not the same as v2[2]
v1[3]=ll, v2[3]=uu
v1[3] is not the same as v2[3]
v1[4]=en, v2[4]=kk
v1[4] is not the same as v2[4]
v1[5]=cy, v2[5]=nn
v1[5] is not the same as v2[5]
Processing 12345 and abc
v1[1]=12, v2[1]=ab
v1[1] is not the same as v2[1]
v1[2]=34, v2[2]=c
v1[2] is not the same as v2[2]
v1[3]=5, v2[3]=
v1[3] is not the same as v2[3]
1 Like