sed replacement, challenge one!!!!

Hi all,

Thanks in advanced.

This question really bothered me much. What i want is to replace any times of repeated 'TB' to 'T', below is example.
It can be fullfil by AWK and perl, but my desire is using SED to realize it.

So here means we treat TB as a whole part, which means 's/TB*/T/' and
's/[TB]*/t/' can not fullfill it.

aTBb              -> aTb
aTBTBc           -> aTc
aTBTBTBd        -> aTd
aTBTTBTBe      -> aTBTTBTBe

Maybe it is too early in the morning but what is different about the last line i.e. 'aTBTTBTBe' ? It has the pattern 'TB' in it. Surely it should change to 'aTTTe"

Try this:
File atd

#!/bin/ksh

for i in $*
do
        z=$(echo $i | grep TT)
        if [ x"$z" = x ]
        then
                z=$(echo $i | sed -e 's/TB/T/g')
                while [ 1 ]
                do
                        z=$(echo $z | sed -e 's/TT/T/g')
                        if [ x"$(echo $z | grep TT)" = x ]
                        then
                                break
                        fi
                done
                echo $i/$z
        else
                echo $i/$i
        fi
done

Run it:

./atd aTBb aTBTBc aTBTBTBd aTBTTBTe
aTBb/aTb
aTBTBc/aTc
aTBTBTBd/aTd
aTBTTBTe/aTBTTBTe

:b:

Hi,

How about the below one,

sed 's/TT/#T#T/g;s/BB/#B#B/g;/#/!s/TB.*TB/T/g;/#/!s/TB/T/g;s/#//g' inp.txt

does it help?

Regards,
Chella

NICE !!! :b::slight_smile: