Hi vordoo,

On Wed, Sep 25, 2013 at 2:24 PM, vordoo <[email protected]> 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??
>
>
This is not hard to do using perl:

shlomif@telaviv1:~$ perl -ple 'my $s1 = "foo"; my $s2 = "bar";
s/((?:\Q$s1\E)|(?:\Q$s2\E))/(($1 eq $s1) ? $s2 : $s1)/ge'
Hello
Hello
The foo bar
The bar foo
bar Hello
foo Hello
foo zoomla
bar zoomla
foo bar ||| bar foo
bar foo ||| foo bar

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.

Regards,

-- Shlomi Fish

-- 
------------------------------------------
Shlomi Fish http://www.shlomifish.org/

Electrical Engineering studies. In the Technion. Been there. Done that.
Forgot a lot. Remember too much.

Please reply to list if it's a mailing list post - http://shlom.in/reply .
_______________________________________________
Linux-il mailing list
[email protected]
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il

Reply via email to