Subbeh
1
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.
zaxxon
2
$ echo abc | perl -pe 's/abc/$&$&/'
abcabc
Edit:
Forget my code on the second example, I got it wrong
1 Like
Subbeh
3
Too bad, the second one is very important for me. Thanks for the first one though
zaxxon
4
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
Subbeh
5
Thanks, that's exactly what I was looking for