Beginner looking for help

Hello,

I am trying to write a script that reads names from a file called input, removes names if they have the same letter next to each other and prints the others.

e.g. Colin & John would be printed
Garry & Lynn would be removed

My thinking is that I read in each name and break it in to a substring and compare each elements neighbour with itself. If at any point array [i]= array[i\+1] the script discards the name and moves on to the next one.

My problem lies with printing the valid names. I have used a nested if statement if [ "$i" == "$((${#s}))" ]; so that once the script has reached the end of a name and it hasn't been discarded it must be valid and therefore should be printed. The script prints nothing and I am at a loss as to why. I would be very grateful if anyone could point out the flaw to me.

Regards,
Colin

***script***

var=`cat input`

for j in $var; do
                s=($j)

                for i in $(seq 0 $((${#s} - 1))); do
                            if [ "${s:$i:1}" == "${s:$[i+1]:1}" ]; then
                                        break
                                        if [ "$i" == "$((${#s}))" ]; then
                                                    echo $j
                                                    break
                                        fi
                            fi
               done
done

Contents of input*
garry
colin
john
gerry
mike
lynn

Depending on the version of grep on your distro, you could do it with a single line:

grep -v '\([[:alpha:]]\)\1' input

Hope this helps.

Thanks in2nix4life,

That worked perfectly. Can you explain how '\([[:alpha:]]\)\1' works to me?

Colin

in this grep have been used backreference expression
in this example

# grep -v '\([[:alpha:]]\)\1' file

-v --> means for except for and print others (does not matches)
\( pattern \) --> tells expression which pattern to referenced
[[:alpha:]] --> Any alphabetic character so [A-Z] or [a-z]
\1 --> our referenced string which we say in \( pattern \)
so \( pattern \) 's equal to \1

finally if any line contains same char ( [A-Z] or [a-z] ) in side by side
for example (Tesst) and do not print this ( grep -v )

and i prefer in sed :wink:

# sed '/\(.\)\1/d' infile

regards
@ygemici