How to recursively replace characters with sed?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' # outputs XoXX
echo XXXX | sed -e 's/XX/XoX/g' # outputs XoXXoX
However, I'm expecting the output to follow the following behavior.XX -> XoX
XXX -> XoXoX
XXXX -> XoXoXoX
Is it possible to achieve the expected behavior with sed alone?
linux bash regex sed
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
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' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' # outputs XoXX
echo XXXX | sed -e 's/XX/XoX/g' # outputs XoXXoX
However, I'm expecting the output to follow the following behavior.XX -> XoX
XXX -> XoXoX
XXXX -> XoXoXoX
Is it possible to achieve the expected behavior with sed alone?
linux bash regex sed
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' # outputs XoXX
echo XXXX | sed -e 's/XX/XoX/g' # outputs XoXXoX
However, I'm expecting the output to follow the following behavior.XX -> XoX
XXX -> XoXoX
XXXX -> XoXoXoX
Is it possible to achieve the expected behavior with sed alone?
linux bash regex sed
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' # outputs XoXX
echo XXXX | sed -e 's/XX/XoX/g' # outputs XoXXoX
However, I'm expecting the output to follow the following behavior.XX -> XoX
XXX -> XoXoX
XXXX -> XoXoXoX
Is it possible to achieve the expected behavior with sed alone?
linux bash regex sed
linux bash regex sed
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 58 mins ago


Ishan Madhusanka
1084
1084
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
You can do:
> echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
XoXoXoX
With:
-e ':loop'
: Create a "loop" label-e 't loop'
: Jump to the "loop" label if previous substitution was successful
add a comment |Â
up vote
1
down vote
In this particular case look-ahead or look-behind would be useful. I think GNU sed
doesn't support these. With pearl
:
perl -ne 's/X(?=X)/Xo/g; print;'
add a comment |Â
up vote
0
down vote
I checked if there is any sort of a flag to make this happen.
Even if that behavior was there it is going to be highly resource consuming.
However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed
expressions.
echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can do:
> echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
XoXoXoX
With:
-e ':loop'
: Create a "loop" label-e 't loop'
: Jump to the "loop" label if previous substitution was successful
add a comment |Â
up vote
2
down vote
accepted
You can do:
> echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
XoXoXoX
With:
-e ':loop'
: Create a "loop" label-e 't loop'
: Jump to the "loop" label if previous substitution was successful
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can do:
> echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
XoXoXoX
With:
-e ':loop'
: Create a "loop" label-e 't loop'
: Jump to the "loop" label if previous substitution was successful
You can do:
> echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
XoXoXoX
With:
-e ':loop'
: Create a "loop" label-e 't loop'
: Jump to the "loop" label if previous substitution was successful
answered 32 mins ago
Gohu
447127
447127
add a comment |Â
add a comment |Â
up vote
1
down vote
In this particular case look-ahead or look-behind would be useful. I think GNU sed
doesn't support these. With pearl
:
perl -ne 's/X(?=X)/Xo/g; print;'
add a comment |Â
up vote
1
down vote
In this particular case look-ahead or look-behind would be useful. I think GNU sed
doesn't support these. With pearl
:
perl -ne 's/X(?=X)/Xo/g; print;'
add a comment |Â
up vote
1
down vote
up vote
1
down vote
In this particular case look-ahead or look-behind would be useful. I think GNU sed
doesn't support these. With pearl
:
perl -ne 's/X(?=X)/Xo/g; print;'
In this particular case look-ahead or look-behind would be useful. I think GNU sed
doesn't support these. With pearl
:
perl -ne 's/X(?=X)/Xo/g; print;'
answered 9 mins ago
Kamil Maciorowski
21.4k155071
21.4k155071
add a comment |Â
add a comment |Â
up vote
0
down vote
I checked if there is any sort of a flag to make this happen.
Even if that behavior was there it is going to be highly resource consuming.
However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed
expressions.
echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
0
down vote
I checked if there is any sort of a flag to make this happen.
Even if that behavior was there it is going to be highly resource consuming.
However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed
expressions.
echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I checked if there is any sort of a flag to make this happen.
Even if that behavior was there it is going to be highly resource consuming.
However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed
expressions.
echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I checked if there is any sort of a flag to make this happen.
Even if that behavior was there it is going to be highly resource consuming.
However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed
expressions.
echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 31 mins ago


Ishan Madhusanka
1084
1084
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Ishan Madhusanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
Ishan Madhusanka is a new contributor. Be nice, and check out our Code of Conduct.
Ishan Madhusanka is a new contributor. Be nice, and check out our Code of Conduct.
Ishan Madhusanka is a new contributor. Be nice, and check out our Code of Conduct.
Ishan Madhusanka is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1366825%2fhow-to-recursively-replace-characters-with-sed%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password