remove CR + trailing spaces

remove CR + trailing spaces

am 19.12.2007 20:08:25 von 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

Re: remove CR + trailing spaces

am 19.12.2007 20:18:12 von krahnj

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

Re: remove CR + trailing spaces

am 19.12.2007 20:33:54 von Ben Morrow

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

Re: remove CR + trailing spaces

am 19.12.2007 20:35:58 von Dan van Ginhoven

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

Re: remove CR + trailing spaces

am 20.12.2007 00:08:57 von rvtol+news

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."