Operating System - HP-UX
1820643 Members
1857 Online
109626 Solutions
New Discussion юеВ

perl - remove last character off each line in file

 
SOLVED
Go to solution
Ratzie
Super Advisor

perl - remove last character off each line in file

How do I remove the last character off each line ( ^M ) in a file using perl
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor

Re: perl - remove last character off each line in file

perl -e's/.$//' file
perl -le's/\s+$//' file

tr '\r' ''
Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor
Solution

Re: perl - remove last character off each line in file

Hi:

I think that our wizard (Merijn) hastely forgot to arm output:

# perl -pe 's/.$//' file

...or if you want to update the file "inplace" (and optionally keep a backup copy):

# perl -pi.old -e 's/.$//' file

...and thus you will have "file.old" as the pre-filtered copy.

For DOS files with not only the carriage-retur (^M) but also an end-of-file "^Z", I use a variation like this:

# perl -pe 's/\r\n/\n/;s/\032//' file

Regards!

...JRF...
Sean Dale
Trusted Contributor

Re: perl - remove last character off each line in file

You could also do one of the following:

use dos2ux to create a new file with the ^M removed

or use vi:

:1,$s/[ctrl-V ctrl-M]//g

where you see [ctrl-v ctrl-m] press control v then control m

the command will remove the ^M in the entire file
Live life everyday
Ratzie
Super Advisor

Re: perl - remove last character off each line in file

Appreciate the help