[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Would like suggestion.



>wpb@jps.net (Bill Becker) wrote:
>  >I have a very large text file with about 90,000 lines in it. A vast majority
>  >of the lines are blank. (In case anyone is interested, it was 
>created with the
>  >bookmanager print-to-file command)
>  >
>  >My question involves is this: When 3 (or "N") or more blank lines are found,
>  >delete them. Leaving 2 in their place, and possibly a marker.
>  >
>  >Is this easy in PERL? (I know everthing in PERL is easy to a lot 
>of you folks,
>  >it's just not intuitively obvious to me, yet!)
>
>Yes, it sure is easy:
>
>---------------------------------------
>open IN, "file.original" or die $!;
>open OUT, ">file.copy"   or die $!;
>
>my $blanks = 0;
>while (<IN>) {
>   $blanks = (length == 1 ? $blanks + 1 : 0);
>   print OUT if $blanks <= 2;
>}
>
>close OUT;
>close IN;
>---------------------------------------

   Or if you have lots of memory:
---------------------------------
#! /usr/local/bin/perl
open (IN, "file.original") or die $!;
local $/;
undef $/;
my $contents = <IN>;
close IN;

$contents =~ s/\n\n\n+/\n\n/g;

open (OUT, ">file.out") or die $!;
print OUT $contents;
close OUT;
---------------------------------
   I generally prefer this method because it allows for easy work to 
be done on the file for many things at the same time.

-Andrew

/*-------------------------------------------------------------
Andrew O. Mellinger                      Direct: (503) 265-1220
Critical Path Software, Inc.               Main: (503) 222-2922
General Specialist                          Fax: (503) 222-3020
mailto:andrew@criticalpath.com      http://www.criticalpath.com
-------------------------------------------------------------*/

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org