Perl sed question

Hi,

I'm trying to use perl the way I use sed.

Lets say I have this command:

$ echo abc | sed 's/abc/&&/'
abcabc

If I try this with perl, I end up with this:

$ echo abc | perl -pe 's/abc/&&/'
&&

How do I make the '&' sign work?

Another thing that I can't get with perl is this:

$ echo bazfoobar | sed '/baz/s/foo/bar/g'
bazbarbar
$ echo bazfoobar | perl -pe '/baz/s/foo/bar/g'
Illegal division by zero at -e line 1, <> line 1.
$ echo abc | perl -pe 's/abc/$&$&/'
abcabc

Edit:
Forget my code on the second example, I got it wrong :wink:

1 Like

Too bad, the second one is very important for me. Thanks for the first one though :slight_smile:

I assume this is the way to go; not sure if there is a shorter sed-stlye way to match first without having to use the if :

$ echo bazfoobar | perl -pe 'if($_ =~ /baz/){s/foo/bar/g}'
bazbarbar

Check out:
http://perldoc.perl.org/perlretut.html

2 Likes

Thanks, that's exactly what I was looking for