On Wed, 8 Oct 2008 08:52:40 -0700, Darrell Fuhriman
<[email protected]> wrote:

> 
> I know, hating perl -- fish in a barrel. I've been doing a lot more  
> stuff in ruby lately, but I still like perl for a few things.  In this  
> case, I had a file which had some spurious carriage returns, which I  
> needed to remove so postgres wouldn't complain about them. As it  
> happens, the carriage returns are also followed by a newline.
> 
> This should be a quick one liner:
> 
> perl -i.bak -ne 's/\r\n//;print' filename

not perlish at all. More perl (but still wrong):

  perl -i.bak -ple's/\r\n//' file

You *remove* all \r\n, replacing it with nothing

  perl -i.bak -ple's/\r\n/\n/' file

would have been better

> So I run it, and now I have a file with no line endings, carriage  
> return or otherwise.

As expected

> That's strange. So I look at it with good old 'od -c' -- sure enough,  
> no other carriage returns than the ones I'm trying to remove,  
> everything else is terminated only with newlines.
> 
> So:
> 
> perl -i.bak -ne 's/\r{1}\n{1}//;print' filename
> 
> And that works. I hate software that tries to be too clever,  

it wasn't being clever. It did exactly what you asked it to do.

> especially in ways where the clever answer is only likely to be right  
> at best a plurality of the time.
> 
> ARRRGGGGGHHHH!!!!

There is enough hate in line ending, even in perl: \n vs $ vs \Z,
optionally combined with the -l command line option and/or the s or m
flags to the regex.

But above sure is not one of those. It's a plain case of PBCAK

-- 
H.Merijn Brand          Amsterdam Perl Mongers  http://amsterdam.pm.org/
using & porting perl 5.6.2, 5.8.x, 5.10.x, 5.11.x on HP-UX 10.20, 11.00,
11.11, 11.23, and 11.31, SuSE 10.1, 10.2, and 10.3, AIX 5.2, and Cygwin.
http://mirrors.develooper.com/hpux/           http://www.test-smoke.org/
http://qa.perl.org      http://www.goldmark.org/jeff/stupid-disclaimers/

Reply via email to