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

Re: [MacPerl] Would like suggestion.



> Greetings!
> 
> 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!)

Here is a very simple way of doing it, however it might be a memory hog:

open (TEXT, "MyFile.file");     ### This opesn up the file.
my @Lines = <TEXT>;             ### This sticks it into an array.
close TEXT;                     ### This closes the file.
$TheFile = join ('', @Lines);   ### This makes that array into a scalar 
value.
$TheFile =~ s/\n{3,}/\n\n/g;    ### This takes 3+ \n and makes it 2 \n 
(\n = newline)
open (TEXT, ">NewFile.file");   ### Getting ready to make the new file.
print TEXT "$TheFile";          ### Puts the contents of $TheFile into 
the new file.
close TEXT;                     ### Closes your new file.

This should work for you.






==========================================
        Real Programmers use Perl

              Mike Eggleston
       nghtstr@michaelsmacshack.com
==========================================


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