I have some code to clean files line by line in perl. One part of the code checks lines for spaces and removes those spaces. Based on my logic, it should also remove lines that are just blank lines, but it does not.
$file = "myfile.txt";
open (IN, $file) || die "Cannot open file ".$file." for read";
@lines=<IN>;
close IN;
open (OUT, ">", $file) || die "Cannot open file ".$file." for write";
foreach $line (@lines)
{
#removes all spaces from lines and but won't remove empty lines (it should).
$line =~ tr/ //d;
#remove double quotes from file
$line =~ s/\x22//g;
print OUT $line;
}
close OUT;
How can I add to this code to remove all lines that are just empty lines? To turn this:
This is "line 1".
This is "line 2".
This is "line 4".
This is "line 5".
into this:
Thisisline1.
Thisisline2.
Thisisline4.
Thisisline5.
The following is what you had originally:
>perl -MO=Deparse -i.bak -nlwe "tr/ //d; print if length"
BEGIN { $^I = ".bak"; }
BEGIN { $^W = 1; }
BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
tr/ //d;
print $_ if length $_;
}
There are significant differences from what you have now. Specifically, you don't account for the newline. You could use the following:
for my $line (@lines) {
$line =~ s/["\s]//g;
print OUT "$line\n" if length($line);
}
\s
matches all whitespace including newlines.