<div dir="ltr">Hi vordoo,<div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Sep 25, 2013 at 2:24 PM, vordoo <span dir="ltr"><<a href="mailto:vordoo@yahoo.com" target="_blank">vordoo@yahoo.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div style="direction:ltr" text="#000000" bgcolor="#FFFFFF">
Hi,<br>
<br>
I know how to: sed -i 's/foo/bar/g' *.txt<br>
<br>
But how do I: replace string foo to bar AND bar to foo in the same
file??<br>
<br></div></blockquote><div><br></div><div>This is not hard to do using perl:<br><br>shlomif@telaviv1:~$ perl -ple 'my $s1 = "foo"; my $s2 = "bar"; s/((?:\Q$s1\E)|(?:\Q$s2\E))/(($1 eq $s1) ? $s2 : $s1)/ge'<br>
Hello<br>Hello<br>The foo bar<br>The bar foo<br>bar Hello<br>foo Hello<br>foo zoomla<br>bar zoomla<br>foo bar ||| bar foo<br>bar foo ||| foo bar<br><br></div><div>Add the -i flag to edit a list of files in place (it in fact originated from an old version of perl and was incorporated into GNU sed - it's not a standard flag.) Can be done using ruby too, but I'm less fluent in it.<br>
<br></div><div>Regards,<br><br></div><div>-- Shlomi Fish<br></div><div> <br clear="all"></div></div>-- <br>------------------------------------------<br>Shlomi Fish <a href="http://www.shlomifish.org/" target="_blank">http://www.shlomifish.org/</a><br>
<br>Electrical Engineering studies. In the Technion. Been there. Done that. Forgot a lot. Remember too much.<br><br>Please reply to list if it's a mailing list post - <a href="http://shlom.in/reply" target="_blank">http://shlom.in/reply</a> .<br>
</div></div>