perl.beginners
[Top] [All Lists]

Re: how to remove ^M character from every line

Subject: Re: how to remove ^M character from every line
From: Xavier Noria
Date: Mon, 3 Sep 2007 15:46:34 +0200
Newsgroups: perl.beginners


Those ^Ms are "\015"s, which is "\r" practically everywhere. Note that line-oriented scripts "almost" work on CRLF text files on Unix because CRLF has a trailing "\n" by chance. I think this is unfortunate because instead of just breaking your program there's the annoying situation where you still read lines but there's something spurious at the end you don't completely understand.

If the script is line-oriented, to read CRLF text files under Unix it is enough to set

  $/ = "\r\n";

and chomp each line as usual.

If you want to preserve newlines instead of chomping then s/\r\n/\n/ instead.

-- fxn


<Prev in Thread] Current Thread [Next in Thread>