equivalent perl for sed command ..newbie question

Discussion in 'Perl Misc' started by walter, Nov 6, 2003.

  1. walter

    walter Guest

    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 !
    walter, Nov 6, 2003
    #1
    1. Advertising

  2. walter

    Ben Morrow Guest

    (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.
    |----------------+---------------| The Levellers, 'Believers'
    Ben Morrow, Nov 6, 2003
    #2
    1. Advertising

  3. walter

    Anno Siegel Guest

    Ben Morrow <> wrote in comp.lang.perl.misc:
    >
    > (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
    Anno Siegel, Nov 6, 2003
    #3
  4. Ben Morrow <> wrote:
    > (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
    Glenn Jackman, Nov 6, 2003
    #4
  5. walter

    Ben Morrow Guest

    Glenn Jackman <> wrote:
    > Ben Morrow <> wrote:
    > > (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
    99p
    Ben Morrow, Nov 6, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NNTP

    sed awk or perl for this?

    NNTP, Sep 11, 2003, in forum: Perl
    Replies:
    13
    Views:
    3,473
    Alan Connor
    Sep 30, 2003
  2. gorda
    Replies:
    2
    Views:
    542
    Andrew Shitov
    Oct 21, 2003
  3. Daniel Fetchinson

    [OFF] sed equivalent of something easy in python

    Daniel Fetchinson, Oct 25, 2010, in forum: Python
    Replies:
    5
    Views:
    402
    Mark Wooding
    Oct 30, 2010
  4. Daniel Fetchinson
    Replies:
    3
    Views:
    317
    Tim Chase
    Oct 27, 2010
  5. gorda
    Replies:
    3
    Views:
    147
    Barry Kimelman
    Oct 21, 2003
Loading...

Share This Page