Come on Perl, rescue me one more time!

Discussion in 'Perl Misc' started by laredotornado, Dec 12, 2009.

  1. Hi,

    I'm using Perl 5.8.8 on Mac 10.5.6. I have a text file with the
    following content pattern ...

    (newline)
    line 1
    line 2
    line 3
    (newline)
    line 1
    line 2
    line 3
    line 4
    line 5
    (new line)
    line 1
    line 2

    What I would like to do is remove the first line immediately after any
    new line (carriage return) and keep all the other lines. How can I do
    this with perl?

    Thanks, - Dave
    laredotornado, Dec 12, 2009
    #1
    1. Advertising

  2. laredotornado <> wrote:
    >I'm using Perl 5.8.8 on Mac 10.5.6. I have a text file with the
    >following content pattern ...
    >
    >(newline)
    >line 1
    >line 2
    >line 3
    >(newline)
    >line 1
    >line 2
    >line 3
    >line 4
    >line 5
    >(new line)
    >line 1
    >line 2
    >
    >What I would like to do is remove the first line immediately after any
    >new line (carriage return) and keep all the other lines. How can I do
    >this with perl?


    Where are you stuck? General algorithmic idea? A specific function that
    you don't know how to use? What code do you have so far that doesn't
    work?

    It's about a 5 line program at most. You may find
    perldoc -f open
    perldoc -f print
    perldoc -f next
    perldoc -q "change, delete"
    as well as if() and while() to be helpful in your task.

    jue
    Jürgen Exner, Dec 12, 2009
    #2
    1. Advertising

  3. >>>>> "laredotornado" == laredotornado <> writes:

    laredotornado> What I would like to do is remove the first line immediately after any
    laredotornado> new line (carriage return) and keep all the other lines. How can I do
    laredotornado> this with perl?

    perl -ne 'print unless (/^$/.../^/) =~ /E/'

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
    Randal L. Schwartz, Dec 13, 2009
    #3
  4. laredotornado

    Guest

    On Dec 12, 2:16 pm, laredotornado <> wrote:
    > I have a text file with the following content pattern ...
    >
    > (newline)
    > line 1
    > line 2
    > line 3
    > (newline)
    > line 1
    > line 2
    > line 3
    > line 4
    > line 5
    > (new line)
    > line 1
    > line 2
    >
    > What I would like to do is remove the first line immediately after any
    > new line (carriage return) and keep all the other lines.  How can I do
    > this with perl?



    Dear Dave,

    I assume by "new line" you really mean a blank line. If I'm right,
    then you can remove the line after every blank line with this simple
    one-line Perl script:

    perl -lpe "length or <>" input.txt > output.txt

    (Warning: This is untested.)

    I hope this helps, Dave.

    -- Jean-Luc
    , Dec 13, 2009
    #4
  5. laredotornado

    C.DeRykus Guest

    On Dec 12, 1:16 pm, laredotornado <> wrote:
    > ...
    >
    > What I would like to do is remove the first line immediately after any
    > new line (carriage return) and keep all the other lines.  How can I do
    > this with perl?
    >


    perl -pe '<> if /^$/' input

    --
    Charles DeRykus
    C.DeRykus, Dec 14, 2009
    #5
  6. laredotornado

    Josef Guest

    Randal L. Schwartz schrieb:
    >>>>>> "laredotornado" == laredotornado <> writes:

    >
    > laredotornado> What I would like to do is remove the first line immediately after any
    > laredotornado> new line (carriage return) and keep all the other lines. How can I do
    > laredotornado> this with perl?
    >
    > perl -ne 'print unless (/^$/.../^/) =~ /E/'


    I prefer
    perl -ne '$.&&print;$.=-length'
    because it is shorter, and less obfuscated¹.
    But OTOH your solution seems more straightforward. ;-)



    yes 'i can'²|perl -lpe '$_="Just Another Perl Hacker"'³,
    Josef

    ad 1) Under the premise that a misuse of a known feature is easier
    to understand as the use of a feature which is unknown for the
    most of us.
    ad 2) …'t get no satisfaction!
    ad 3) use warnings; use Acme::Your 'Keyboard'; your $key="\cC";
    PS: The (non·)solutions from JeanLuc & DeRykus looks nice, but …
    Maybe
    perl -pe 'print,<>,chop if /^$/'
    perl -pe "length>1 or print,<>,chop"
    is near enough.
    Josef, Dec 15, 2009
    #6
    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. Michael
    Replies:
    4
    Views:
    410
    Matt Hammond
    Jun 26, 2006
  2. Merciadri Luca
    Replies:
    4
    Views:
    809
  3. Replies:
    3
    Views:
    186
    Josef Moellers
    Nov 1, 2007
  4. Steven D'Aprano
    Replies:
    0
    Views:
    90
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    82
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page