Re: Delete multiple blank lines

2004-01-20 Thread James Edward Gray II
On Jan 20, 2004, at 1:01 PM, Paul Johnson wrote:

perl -i~ -p00e0 file1 file2 ...
Wow!  I've been over here trying to explain to myself why that works.  
Help me out a little.

Broken down those switches are:

-i~ -p -00e0

Right?

No problems with the first two here.  The third sets the input record 
separator to 0e0, I think.  My 'perlrun' said 00 was a special value 
used to get "paragraph mode".  I'm going to assume 0e0 does the same 
thing.

Here's where I'm fuzzy though, does "paragraph mode" only leave one \n 
at the end of $_?  I didn't know that.  Is it true even if you set if 
the usual way?

local $/ = '';

One last question, does "paragraph mode" consider lines with only 
spaces "blank lines" or must it be back-to-back newlines?

Thanks for the lesson!

James

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Delete multiple blank lines

2004-01-20 Thread James Edward Gray II
On Jan 20, 2004, at 12:39 PM, Lewick, Taylor wrote:

Hi all, I could use some help with an expression or code to delete
multiple blank lines and replace it with only one blank line...
I know how this can be done in sed, but am not sure how to do it in
perl...
Well, if slurping is okay (read: file size is reasonable), I would 
probably use something like this (untested code):

#!/usr/bin/perl

use strict;
use warnings;
die "Usage:  perl script_name OLD_FILE NEW_FILE\n" unless @ARGV == 2;

open OLD, '<', shift() or die "File error:  $!\n";
my $text = join '', ;
close OLD;
$text =~ s/\n(?:\s*\n)+/\n/g;

open NEW, '<', shift() or die "File error:  $!\n";
print NEW $text;
close NEW;
__END__

Hope that helps.

James

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Re: Delete multiple blank lines

2004-01-20 Thread Paul Johnson
On Tue, Jan 20, 2004 at 12:39:55PM -0600, Lewick, Taylor wrote:

> Hi all, I could use some help with an expression or code to delete
> multiple blank lines and replace it with only one blank line...
> 
> I know how this can be done in sed, but am not sure how to do it in
> perl...

perl -i~ -p00e0 file1 file2 ...

perldoc perlrun

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Delete multiple blank lines

2004-01-20 Thread Wiggins d Anconia


> Hi all, I could use some help with an expression or code to delete
> multiple blank lines and replace it with only one blank line...
> 
> I know how this can be done in sed, but am not sure how to do it in
> perl...
> 
> Thanks,
> Taylor

Well you could do this the simple way, use a switch that toggles on/off
whether the last line you checked was empty.  If the switch is on, and
the current line is empty simply skip it. If the switch is off and the
current line is empty then keep it, toggle the switch and move on.

KISS,

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Delete multiple blank lines

2004-01-20 Thread Lewick, Taylor
Hi all, I could use some help with an expression or code to delete
multiple blank lines and replace it with only one blank line...

I know how this can be done in sed, but am not sure how to do it in
perl...

Thanks,
Taylor

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>