remove CR + trailing spaces

D

Dan van Ginhoven

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
 
J

John W. Krahn

Dan said:
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
 
B

Ben Morrow

Quoth "Dan van Ginhoven said:
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
 
D

Dr.Ruud

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,131
Latest member
IsiahLiebe
Top