Hi PerlMonks, at best, I am a novice to Perl, who is seeking to remove large chunks of whitespace (i.e., anything other than a character) between lines of a text file. This site contains a thread dating back to 2000, which seems to address this issue. Some Monks suggest the use of substitution pattern matching, while others suggest WHILE loops. I am attempting to use a while loop in the code below (but remain completely open to other ideas). However, I have no need for user input(i.e., do not need <STDIN>), and am less than clueless for how to use "print if (!/^\s*$/)" to remove the whitespace and write/save the result to the file. Apologize for such a simple problem. Grateful for any ideas. Thank you!
#! /usr/bin/perl -w use strict; use warnings; use lib "c:/strawberry/perl/site/lib"; my $files_dir = 'F:\research\SEC filings 10K and 10Q\Data\Filing Docs\ +2009\Test Data\HTML Clean'; my $write_dir = 'F:\research\SEC filings 10K and 10Q\Data\Filing Docs\ +2009\Test Data\HTML Clean\Non Word Strip'; opendir (my $dir_handle, $files_dir); while (my $filename = readdir($dir_handle)) { next unless -f $files_dir.'/'.$filename; print "Procesing $filename\n"; open my $fh_in, '<', $files_dir.'/'.$filename or die "failed to open '$filename' for read"; open my $fh_out, '>', $write_dir.'/'.$filename or die "failed to open '$filename' for write"; my $count=0; while (my $line = <$fh_in>) { my $text = $line; chomp ($text); #Strip/remove whitespace between lines of text file; while (<STDIN>) { print if (!/^\s*$/); } print $fh_out "$text\n"; #Save stripped results; } ++$count; print "$count lines read from $filename\n;" }
In reply to Remove whitespace between lines by wrkrbeee
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |