Remove blank lines from text file

Discussion in 'Perl Misc' started by richardkreidl@northwesternmutual.com, Sep 10, 2005.

  1. Guest

    I need to remove Carriage returns and blank lines from a text file. The
    CR works ok, but I'm not sure how to remove blank lines.

    This command line works: perl -i -pwe `$_="" unless /\s/' filea...
    fileb

    I would like to use it in my script below.

    #!/opt/perl/bin/perl
    open(IN,"Input.txt") || die("Can't open file");
    open(OUT,">Output.txt") || die("Can't open file");

    while(<IN>) {
    chomp;
    $text=<IN>;
    $text=~ s/[\r]//gs;
    print OUT $text
    }
    close(IN);
    close(OUT);
     
    , Sep 10, 2005
    #1
    1. Advertising

  2. Matija Papec Guest

    X-Ftn-To:

    <> wrote:
    >I need to remove Carriage returns and blank lines from a text file. The
    >CR works ok, but I'm not sure how to remove blank lines.
    >
    >This command line works: perl -i -pwe `$_="" unless /\s/' filea...
    >fileb



    perl -i -ne "tr|\r||d; next if /^\s*$/;print" filea



    --
    Matija
     
    Matija Papec, Sep 10, 2005
    #2
    1. Advertising

  3. Guest

    It works, but how would I do in my script??
     
    , Sep 10, 2005
    #3
  4. Guest

    This command works great:
    perl -i -ne "tr|\r||d; next if /^\s*$/;print" filea
    How do I use it in my script.

    #!/opt/perl/bin/perl
    open(IN,"Input.txt") || die("Can't open file");
    open(OUT,">Output.txt") || die("Can't open file");

    while(<IN>) {
    chomp;
    $text=<IN>;
    $text=~ s/[\r]//gs;
    print OUT $text
    }
    close(IN);
    close(OUT);
     
    , Sep 10, 2005
    #4
  5. Guest

    I figured it out.
     
    , Sep 10, 2005
    #5
  6. Big and Blue Guest

    wrote:

    > I need to remove Carriage returns and blank lines from a text file. The
    > CR works ok, but I'm not sure how to remove blank lines.
    >
    > This command line works: perl -i -pwe `$_="" unless /\s/' filea...
    > fileb


    #!/usr/bin/perl

    $/="\r\n";
    $\="\n";

    while (<>) {chomp; print if (/\S/)};


    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
     
    Big and Blue, Sep 10, 2005
    #6
  7. Tintin Guest

    <> wrote in message
    news:...
    >I figured it out.


    Figured what out?

    Certainly not how to quote correctly.
     
    Tintin, Sep 11, 2005
    #7
  8. <> wrote:

    > This command works great:
    > perl -i -ne "tr|\r||d; next if /^\s*$/;print" filea
    > How do I use it in my script.


    #!/opt/perl/bin/perl

    $infile = "Input.txt";
    $outfile = "Output.txt";

    open(IN, $infile) || die("$infile: open failed");
    open(OUT, ">$outfile") || die("$outfile: open failed");

    while (<IN>) {
    next unless /\S/;
    tr/\r//d;
    print OUT $_ || die("$outfile: print failed");
    }
    close(IN) || die("$infile: close failed");
    close(OUT) || die("$outfile: close failed");

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
     
    Peter J. Acklam, Sep 12, 2005
    #8
    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. Ruben
    Replies:
    6
    Views:
    610
    Carlos Ribeiro
    Sep 13, 2004
  2. Peter Otten

    Re: how to remove the blank lines?

    Peter Otten, Dec 8, 2006, in forum: Python
    Replies:
    1
    Views:
    664
    Peter Otten
    Dec 9, 2006
  3. Jack Wang
    Replies:
    5
    Views:
    106
    Mario D'Alessio
    Feb 15, 2008
  4. Cah Sableng
    Replies:
    0
    Views:
    263
    Cah Sableng
    Apr 23, 2007
  5. Replies:
    6
    Views:
    150
    Thomas 'PointedEars' Lahn
    Jan 27, 2008
Loading...

Share This Page