equivalent perl for sed command ..newbie question

W

walter

Hi,

I just started learning perl.

Now I know

perl -p -e 's/exp1/exp2/g' file

is equivalent to

sed -e 's/exp1/exp2/g' file

But I can't realize what's the perl equivalent to

sed -e '/exp1/,/exp2/d' file

I tried to mimic it using

perl -p -e '/exp1/,/exp2/d'

but, of course, it didn't work...

Yes...I know it's a dumb question....Sorry!

Thanks for your time and patience !
 
B

Ben Morrow

Now I know

perl -p -e 's/exp1/exp2/g' file

is equivalent to

sed -e 's/exp1/exp2/g' file

Except that Perl's regexes are better :)
But I can't realize what's the perl equivalent to

sed -e '/exp1/,/exp2/d' file

perl -ne'print unless /exp1/.../exp2/'

See perldoc perlop for .. and ..., and the differences between them.

Ben
 
A

Anno Siegel

Ben Morrow said:
Except that Perl's regexes are better :)


perl -ne'print unless /exp1/.../exp2/'

See perldoc perlop for .. and ..., and the differences between them.

Alternatively, use the program s2p, which comes with the Perl distribution.
It translates sed scripts to Perl scripts. In this case it comes up with
a whole lot of code for what can apparently be done in a single line, but
it does so automatically.

Anno
 
G

Glenn Jackman

Ben Morrow said:
perl -ne'print unless /exp1/.../exp2/'

See perldoc perlop for .. and ..., and the differences between them.

Ah, the lightbulb illuminates. Thanks for that example: I now
understand the .. operator in a scalar context. Still a bit fuzzy on
the difference with ... though. Can someone provide another example?
This outputs the same results whether I use .. or ...:

#!/usr/local/bin/perl -w
use strict;
while (<DATA>) {
print if /4/../7/;
}
__DATA__
1
2
3
4
5
6
7
8
9
 
B

Ben Morrow

Glenn Jackman said:
Ah, the lightbulb illuminates. Thanks for that example: I now
understand the .. operator in a scalar context. Still a bit fuzzy on
the difference with ... though. Can someone provide another example?
This outputs the same results whether I use .. or ...:

<snip>

~% echo "12345467" | perl -lne'BEGIN{$/=\1}; print if /4/.../4/'
4
5
4
~% echo "12345467" | perl -lne'BEGIN{$/=\1}; print if /4/../4/'
4
4
~%

Note that the ... example goes off-on-off whereas the .. example goes
off-onoff-onoff.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top