Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > remove CR + trailing spaces

Reply
Thread Tools

remove CR + trailing spaces

 
 
Dan van Ginhoven
Guest
Posts: n/a
 
      12-19-2007
Hi!

I had some files on my Linux server containing Dos/Win style CRLF and also
trailing spaces on most lines.

I ran this one-liner perl on them:
cat localfn | perl -p -e "s/\r//;s/\s*$//;" > tempname
To my surprise both CR and LF was removed and I ended up with a file with
just one long line.

When I changed the one-liner to
cat localfn | perl -p -e "s/\r//;s/\s*$/\n/;" > tempname
it came out OK.

What am I missing?
Can any point me to the appropriate FAQ or man page please.


/dg


 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      12-19-2007
Dan van Ginhoven wrote:
>
> I had some files on my Linux server containing Dos/Win style CRLF and also
> trailing spaces on most lines.
>
> I ran this one-liner perl on them:
> cat localfn | perl -p -e "s/\r//;s/\s*$//;" > tempname
> To my surprise both CR and LF was removed and I ended up with a file with
> just one long line.
>
> When I changed the one-liner to
> cat localfn | perl -p -e "s/\r//;s/\s*$/\n/;" > tempname
> it came out OK.
>
> What am I missing?


The character class \s includes both the \r character and the \n
character.

perldoc perlre


You could simplify that to:

perl -lpe "s/\s+\z//" localfn > tempname


Or if you want to modify localfn "in-place":

perl -i.bak -lpe "s/\s+\z//" localfn



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      12-19-2007

Quoth "Dan van Ginhoven" <(E-Mail Removed)>:
>
> I had some files on my Linux server containing Dos/Win style CRLF and also
> trailing spaces on most lines.
>
> I ran this one-liner perl on them:
> cat localfn | perl -p -e "s/\r//;s/\s*$//;" > tempname


Useless Use Of Cat.

perl ... < localfn > tempname

or use perl -i.

> To my surprise both CR and LF was removed and I ended up with a file with
> just one long line.


\s matches \n, so this will remove all LFs.

> When I changed the one-liner to
> cat localfn | perl -p -e "s/\r//;s/\s*$/\n/;" > tempname
> it came out OK.


This will remove any blank lines, as well. You want

s/[\r\t ]+$//;

or (with 5.10)

s/[\r\h]+$//;

or use perl -l, which will remove the "\n" before doing the s///, and
add it back on afterwards.

Ben

 
Reply With Quote
 
Dan van Ginhoven
Guest
Posts: n/a
 
      12-19-2007
Thanks John.
>
> The character class \s includes both the \r character and the \n
> character.
>



 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-19-2007
Dan van Ginhoven schreef:

> cat localfn | perl -p -e "s/\r//;s/\s*$//;" > tempname
> To my surprise both CR and LF was removed and I ended up with a file
> with just one long line.


Consider [[:blank:]] in stead of \s.

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD to allow A-Z 0-9 and spaces, but not leading/trailing spaces johkar XML 2 12-10-2009 09:24 AM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
Remove trailing spaces in ecilpse pd Java 3 12-07-2007 12:32 PM
add ability to strip off leading/trailing spaces from email regularExpressionValidator? Les Caudle ASP .Net 0 03-07-2004 12:20 PM



Advertisments