This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Easy? sed syntax


Please can anybody help with a simply-stated sed problem? I realise this is
not Cygwin-specific, but it is Cygwin-relevant because it is required in
order to write a script to mount a portable Cygwin system on any host
machine, so I hope it's all right to ask for help here.

The problem reduces to:

In any string (eg "xaaabababbbxaabbbabx") remove all instances of "ab" and
keep on doing this as long as you can.

Do it once:

    echo xaaabababbbxaabbbabx | sed 's/ab//g'
    xaabbxabbx

The essential issue is that that removal of all instances of "ab" has
created new instances of "ab" in the reduced string, so it needs to be done
again:

    echo xaaabababbbxaabbbabx | sed 's/ab//g' | sed 's/ab//g'
    xabxbx

and it needs to be done a 3rd time to reach the irreducible string

    echo xaaabababbbxaabbbabx | sed 's/ab//g' | sed 's/ab//g' | sed
's/ab//g'
    xxbx

Different initial strings might need more passes through sed, and it's not
possible to forecast how many passes will be needed (well, it is, but only
by a complex counting algorithm). Is there a way with a single sed command

    echo xaaabababbbxaabbbabx REPEAT{| sed 's/ab//g'}

that I can "repeat the pipe as many times as possible" and then stop?

Don't know whether this is obvious/ simple/ difficult/ possible/ or any of
these: thank you. 

Fergus


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]