FAQ
Hi all, I could use some help with an expression or code to delete
multiple blank lines and replace it with only one blank line...

I know how this can be done in sed, but am not sure how to do it in
perl...

Thanks,
Taylor

Search Discussions

  • Wiggins d Anconia at Jan 20, 2004 at 6:57 pm

    Hi all, I could use some help with an expression or code to delete
    multiple blank lines and replace it with only one blank line...

    I know how this can be done in sed, but am not sure how to do it in
    perl...

    Thanks,
    Taylor
    Well you could do this the simple way, use a switch that toggles on/off
    whether the last line you checked was empty. If the switch is on, and
    the current line is empty simply skip it. If the switch is off and the
    current line is empty then keep it, toggle the switch and move on.

    KISS,

    http://danconia.org
  • Paul Johnson at Jan 20, 2004 at 7:02 pm

    On Tue, Jan 20, 2004 at 12:39:55PM -0600, Lewick, Taylor wrote:

    Hi all, I could use some help with an expression or code to delete
    multiple blank lines and replace it with only one blank line...

    I know how this can be done in sed, but am not sure how to do it in
    perl...
    perl -i~ -p00e0 file1 file2 ...

    perldoc perlrun

    --
    Paul Johnson - paul@pjcj.net
    http://www.pjcj.net
  • James Edward Gray II at Jan 20, 2004 at 7:37 pm

    On Jan 20, 2004, at 1:01 PM, Paul Johnson wrote:

    perl -i~ -p00e0 file1 file2 ...
    Wow! I've been over here trying to explain to myself why that works.
    Help me out a little.

    Broken down those switches are:

    -i~ -p -00e0

    Right?

    No problems with the first two here. The third sets the input record
    separator to 0e0, I think. My 'perlrun' said 00 was a special value
    used to get "paragraph mode". I'm going to assume 0e0 does the same
    thing.

    Here's where I'm fuzzy though, does "paragraph mode" only leave one \n
    at the end of $_? I didn't know that. Is it true even if you set if
    the usual way?

    local $/ = '';

    One last question, does "paragraph mode" consider lines with only
    spaces "blank lines" or must it be back-to-back newlines?

    Thanks for the lesson!

    James
  • James Edward Gray II at Jan 20, 2004 at 7:04 pm

    On Jan 20, 2004, at 12:39 PM, Lewick, Taylor wrote:

    Hi all, I could use some help with an expression or code to delete
    multiple blank lines and replace it with only one blank line...

    I know how this can be done in sed, but am not sure how to do it in
    perl...
    Well, if slurping is okay (read: file size is reasonable), I would
    probably use something like this (untested code):

    #!/usr/bin/perl

    use strict;
    use warnings;

    die "Usage: perl script_name OLD_FILE NEW_FILE\n" unless @ARGV == 2;

    open OLD, '<', shift() or die "File error: $!\n";
    my $text = join '', <OLD>;
    close OLD;

    $text =~ s/\n(?:\s*\n)+/\n/g;

    open NEW, '<', shift() or die "File error: $!\n";
    print NEW $text;
    close NEW;

    __END__

    Hope that helps.

    James

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJan 20, '04 at 6:41p
activeJan 20, '04 at 7:37p
posts5
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase