Remove blank lines from text file

R

richardkreidl

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);
 
M

Matija Papec

X-Ftn-To: (e-mail address removed)

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
 
R

richardkreidl

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);
 
B

Big and Blue

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/)};
 
P

Peter J. Acklam

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top