I have a text 'ab' whose content is composed of a,b letter. I want to change a to b, b to a at once by using s command in ed. But if I change them step by step, as you know, all the text will be changed into only a or only b. Is there any way to change a to b and b to a at the same time?


Not natively, no -- it lacks the transliterate command you see in sed (y) and perl (tr/// aka y///) -- but you could fake it with the transliterate command itself:

ed -s input <<< $'e !tr ab ba < %\nw\nq'

The e command replaces the contents of the file with the results of the shell escape command; that command transliterates a to b and b to a and is given the current filename as input. The new file is then written to disk; then we quit ed.

| improve this answer | |

You do it the same as you swap two variables in C or BASIC: tmp = a; a = b; b = tmp ;-)



Replace FOO above with any char or text which doesn't appear in your line.

| improve this answer | |

Not "at the same time", but you can do it in a series of steps.

The first step is to tag all the things needing to be changed.

Second step is to change all tagged a to b.

Third step is to change all tagged b to a.

So the question then becomes how can I tag the things to change?

There is an interesting technique which says you can use a letter a (one of the two characters you want to swap) to do the tagging.


For this input

aaaa aaab aaba aabb abaa abab abba abbb baaa baab baba babb bbaa bbab bbba bbbb

the steps produce

aaaaaaaa aaaaaaab aaaaabaa aaaaabab aaabaaaa aaabaaab aaababaa aaababab abaaaaaa abaaaaab abaaabaa abaaabab ababaaaa ababaaab abababaa abababab
bbbb bbbab bbabb bbabab babbb babbab bababb bababab abbbb abbbab abbabb abbabab ababbb ababbab abababb abababab
bbbb bbba bbab bbaa babb baba baab baaa abbb abba abab abaa aabb aaba aaab aaaa

which is the desired result.

Obviously if there is a character that is known not to be in the input, for example ctrl-A, you can also do the change in 3 steps, first change all a to ctrl-A, then change all b to a and then change all ctrl-a to b.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.