wrkrbeee has asked for the wisdom of the Perl Monks concerning the following question:
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;" }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Remove whitespace between lines
by Laurent_R (Canon) on Feb 03, 2015 at 18:05 UTC | |
by wrkrbeee (Scribe) on Feb 03, 2015 at 18:41 UTC | |
by poj (Abbot) on Feb 03, 2015 at 18:51 UTC | |
by wrkrbeee (Scribe) on Feb 03, 2015 at 18:55 UTC | |
by Marshall (Canon) on Feb 04, 2015 at 01:04 UTC | |
by Anonymous Monk on Feb 03, 2015 at 18:58 UTC | |
Re: Remove whitespace between lines
by Your Mother (Archbishop) on Feb 03, 2015 at 18:13 UTC | |
Re: Remove whitespace between lines
by Anonymous Monk on Feb 03, 2015 at 18:05 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |