Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Remove blank lines from text file

Reply
Thread Tools

Remove blank lines from text file

 
 
richardkreidl@northwesternmutual.com
Guest
Posts: n/a
 
      09-10-2005
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);

 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      09-10-2005
X-Ftn-To: http://www.velocityreviews.com/forums/(E-Mail Removed)

<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
richardkreidl@northwesternmutual.com
Guest
Posts: n/a
 
      09-10-2005
It works, but how would I do in my script??

 
Reply With Quote
 
richardkreidl@northwesternmutual.com
Guest
Posts: n/a
 
      09-10-2005
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);

 
Reply With Quote
 
richardkreidl@northwesternmutual.com
Guest
Posts: n/a
 
      09-10-2005
I figured it out.

 
Reply With Quote
 
Big and Blue
Guest
Posts: n/a
 
      09-10-2005
(E-Mail Removed) 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.
 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      09-11-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>I figured it out.


Figured what out?

Certainly not how to quote correctly.


 
Reply With Quote
 
Peter J. Acklam
Guest
Posts: n/a
 
      09-12-2005
<(E-Mail Removed)> 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;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any Idea why this code doesn't remove all the blank lines? Jack Wang Perl Misc 5 02-15-2008 03:54 PM
Remove trailing newlines (blank lines) ??? lihao0129@gmail.com Javascript 6 01-27-2008 10:53 PM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
Re: how to remove the blank lines? Peter Otten Python 1 12-09-2006 07:43 AM
Not able to read blank lines and spaces on a small text file Ruben Python 6 09-13-2004 03:30 PM



Advertisments