replace string foo to bar AND bar to foo in the same file

replace string foo to bar AND bar to foo in the same file

Omer Zak w1 at zak.co.il
Wed Sep 25 14:30:55 IDT 2013


Some context is missing - is it an one-shot job to be carried manually,
or do you plan to run it automatically from a script?

In principle, you first do:
  egrep thirdstring *.txt
to make that the string 'thirdstring' does not exist anywhere.
A script would need a way to select another 'thirdstring' if the
original one is already in use.

Then you perform three sed's:
sed -i 's/foo/thirdstring/g' *.txt
sed -i 's/bar/foo/g' *.txt
sed -k 's/thirdstring/bar/g' *.txt

DISCLAIMER: I did not actually run the above code.
--- Omer


On Wed, 2013-09-25 at 14:24 +0300, vordoo wrote:
> Hi,
> 
> I know how to: sed -i 's/foo/bar/g' *.txt
> 
> But how do I: replace string foo to bar AND bar to foo in the same
> file??

-- 
"I grew up in Chicago, learned to vote, then moved to Florida in time
for the 2000 election. In both places I was told I did an excellent job,
as did my father, grandfather, and in 2000, my great-great grandfather,
who cast 277 votes."                             S. Litt

My own blog is at http://www.zak.co.il/tddpirate/

My opinions, as expressed in this E-mail message, are mine alone.
They do not represent the official policy of any organization with which
I may be affiliated in any way.
WARNING TO SPAMMERS:  at http://www.zak.co.il/spamwarning.html




More information about the Linux-il mailing list