FAQ
I want to slurp a file and make multiple blank lines into single blank
lines. Here is my code:

#! perl -w
use strict;

my $file = "test.fmt";

open (IN, $file) or die;
open (OUT, ">>test.txt");

my $text;

{
local $/;
$text = <IN>;
close IN;
}

# $text =~ s/\0{2,}/\0\0/mg; # this removes all the blank lines

# $text =~ s/(^$^$)(^$)+/$1/mg; # also removes all blank lines and
complains

# $text =~ s/(\n\n)(\n)+/$1/mg; # no improvement here either

print OUT "$file\n\n$text\n******************";
close OUT;


I have tried a number of substitutions but haven't found the correct
formulation.

surely I am missing something simple here.

Larry

Search Discussions

  • Nikola Janceski at Jan 31, 2002 at 5:37 pm
    you want to treat $text as a single string
    use
    $text =~ s/\n+/\n/sg; # use /mg if you want to treat the string as multiple
    lines.

    -----Original Message-----
    From: Moore, Larry
    Sent: Thursday, January 31, 2002 12:33 PM
    To: beginners@perl. org (E-mail)
    Subject: replacing multiple blank lines with a single blank line


    I want to slurp a file and make multiple blank lines into single blank
    lines. Here is my code:

    #! perl -w
    use strict;

    my $file = "test.fmt";

    open (IN, $file) or die;
    open (OUT, ">>test.txt");

    my $text;

    {
    local $/;
    $text = <IN>;
    close IN;
    }

    # $text =~ s/\0{2,}/\0\0/mg; # this removes all the blank lines

    # $text =~ s/(^$^$)(^$)+/$1/mg; # also removes all blank lines and
    complains

    # $text =~ s/(\n\n)(\n)+/$1/mg; # no improvement here either

    print OUT "$file\n\n$text\n******************";
    close OUT;


    I have tried a number of substitutions but haven't found the correct
    formulation.

    surely I am missing something simple here.

    Larry



    ----------------------------------------------------------------------------
    --------------------
    The views and opinions expressed in this email message are the sender's
    own, and do not necessarily represent the views and opinions of Summit
    Systems Inc.
  • Jeff 'japhy' Pinyan at Jan 31, 2002 at 9:16 pm
    On Jan 31, Nikola Janceski said:
    you want to treat $text as a single string
    use
    $text =~ s/\n+/\n/sg; # use /mg if you want to treat the string as multiple
    lines.
    Your information about /s and /m is wrong. /s changes how . matches, and
    there is no . in your regex. /m changes how ^ and $ work, and there are
    no ^ or $ in your regex.

    $text =~ s/\n+/\n/g;

    is fine, as is:

    $text =~ s/\n\n+/\n/g;

    as is:

    $text =~ tr/\n//s;

    --
    Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/
    RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
    ** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
  • Curtis Poe at Jan 31, 2002 at 6:06 pm

    --- "Moore, Larry" wrote:
    I want to slurp a file and make multiple blank lines into single blank
    lines. Here is my code:

    #! perl -w
    use strict;

    my $file = "test.fmt";

    open (IN, $file) or die;
    open (OUT, ">>test.txt");

    my $text;

    {
    local $/;
    $text = <IN>;
    close IN;
    }

    # $text =~ s/\0{2,}/\0\0/mg; # this removes all the blank lines

    # $text =~ s/(^$^$)(^$)+/$1/mg; # also removes all blank lines and
    complains

    # $text =~ s/(\n\n)(\n)+/$1/mg; # no improvement here either

    print OUT "$file\n\n$text\n******************";
    close OUT;
    I'm not sure what you mean by "blank lines". I would take that to mean that lines that were all
    whitespace should also be included (e.g., a single tab with three spaces on a line would be a
    blank line). If so, the following should work:

    <perl>
    #! perl -w
    use strict;

    my $text = '';

    {
    local $/;
    $text = <DATA>;
    }

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

    print $text;
    __DATA__
    test

    asdf






    test

    test
    </perl>

    Cheers,
    Curtis "Ovid" Poe

    =====
    "Ovid" on http://www.perlmonks.org/
    Someone asked me how to count to 10 in Perl:
    push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
    shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A

    __________________________________________________
    Do You Yahoo!?
    Great stuff seeking new owners in Yahoo! Auctions!
    http://auctions.yahoo.com
  • John W. Krahn at Feb 2, 2002 at 2:21 am

    Larry Moore wrote:

    I want to slurp a file and make multiple blank lines into single blank
    lines. Here is my code:

    [snip code]

    I have tried a number of substitutions but haven't found the correct
    formulation.

    #! perl -w
    use strict;

    my $file = 'test.fmt';

    open IN, $file or die "Cannot open $file: $!";
    open OUT, '>> test.txt' or die "Cannot open test.txt: $!";

    print OUT "$file\n\n";
    {
    local $/ = ''; # paragraph mode
    print OUT <IN>;
    }
    close IN;
    print OUT '******************';
    close OUT;



    John
    --
    use Perl;
    program
    fulfillment

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJan 31, '02 at 5:33p
activeFeb 2, '02 at 2:21a
posts5
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase