13
Is it possible to replace occurrences of a character sequence recursively without iterating over the same sequence again?
By performing a sed
as in the following scenarios I can get the mentioned output.
$ echo XX | sed -e 's/XX/XoX/g'
XoX
$ echo XXX | sed -e 's/XX/XoX/g'
XoXX
$ echo XXXX | sed -e 's/XX/XoX/g'
XoXXoX
However, I'm expecting the output to follow the following behavior.
Input:
XX
XXX
XXXX
Expected output:
XoX
XoXoX
XoXoXoX
Is it possible to achieve the expected behavior with sed alone?
1This assumes that input strings consist of some number of
X
and nothing else. Both solutions fail if there are other characters present... – AnoE – 2018-10-16T10:50:57.727@AnoE In the second sample, that is fixed with a simple replacement of
X
by.
. Please see edit. – Digital Trauma – 2018-10-16T19:09:41.630Not equivalent to the case the OP gave. He gave the exact REs he needs (change occurrences of XX in a string). Your versions only give the same result as his for the exact same input strings he gave; not for generic input strings. – AnoE – 2018-10-16T20:18:21.903