Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > equivalent perl for sed command ..newbie question

Reply
Thread Tools

equivalent perl for sed command ..newbie question

 
 
walter
Guest
Posts: n/a
 
      11-06-2003
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 !
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      11-06-2003

http://www.velocityreviews.com/forums/(E-Mail Removed) (walter) wrote:
> 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

--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
(E-Mail Removed) |----------------+---------------| The Levellers, 'Believers'
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      11-06-2003
Ben Morrow <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> (E-Mail Removed) (walter) wrote:
> > 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.


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
 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      11-06-2003
Ben Morrow <(E-Mail Removed)> wrote:
> (E-Mail Removed) (walter) wrote:
> > sed -e '/exp1/,/exp2/d' file

>
> 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



--
Glenn Jackman
NCF Sysadmin
(E-Mail Removed)
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-06-2003

Glenn Jackman <(E-Mail Removed)> wrote:
> Ben Morrow <(E-Mail Removed)> wrote:
> > (E-Mail Removed) (walter) wrote:
> > > sed -e '/exp1/,/exp2/d' file

> >
> > 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 ...:


<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

--
EAT
KIDS (...er, whoops...)
FOR (E-Mail Removed)
99p
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[OFF] sed equivalent of something easy in python Daniel Fetchinson Python 5 10-30-2010 03:14 PM
Re: [OFF] sed equivalent of something easy in python Daniel Fetchinson Python 3 10-27-2010 02:08 PM
using perl on the command line, like sed or awk gorda Perl Misc 3 10-21-2003 01:50 PM
using perl on the command line, like sed or awk gorda Perl 2 10-21-2003 06:38 AM
sed awk or perl for this? NNTP Perl 13 09-30-2003 06:49 AM



Advertisments