icg has asked for the wisdom of the Perl Monks concerning the following question:

How can I remove carriage return character which is present in the middle of a stream of characters? The source of the stream of characters is a file with no line breaks. The data causing the problem looks something like this: abcdef^Mghijkl ...

Replies are listed 'Best First'.
Re: Remove Carriage Return
by japhy (Canon) on Sep 16, 2005 at 12:54 UTC
    The same way you remove it anywhere else in a string. s/\r// will get rid of ONE carriage return. tr/\r//d will get rid of ALL of them (more efficiently than s/\r+//g would).

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Remove Carriage Return
by philcrow (Priest) on Sep 16, 2005 at 12:56 UTC
    Yes. This might be what you mean:
    $string =~ s/\n//;
    That will remove the first newline it sees. When mixing platforms you might have to deal with \r also. If you want to remove all the newlines add a g switch:
    $string =~ s/\n//g;
    Consider replacing the newline with something smaller, like a space.

    So newline is a single character (at least on unix) and you can match it with \n.

    Phil

    Update: Hue-Bond pointed out an error in wording. The issue with \r arrises when you process a file on a different system than the one that generated it. Generally \n will match whatever the native new line combination is, but it will not necessarily match if the file came from a foreign system.

      On non-unix boxes you might have to deal with \r also.

      I was taught that \n means "new line" so it maps to \x0a on Unix, \x0d on Macs and \x0d\x0a on Win*. Thus, it would be enough to get rid of \n.

      --
      David Serrano

Re: Remove Carriage Return
by sh1tn (Priest) on Sep 16, 2005 at 15:02 UTC
Re: Remove Carriage Return
by Util (Priest) on Sep 16, 2005 at 16:02 UTC

    The regex answers already given are the best, most "Perlish" solutions.
    Here are three other ways to remove or replace a single character, using index and substr.

    my $data1 = "abc\ndef"; my $data2 = "foobar\nbaz"; my $data3 = "Albus\nPercival\nWulfric\nBrian\nDumbledore\n"; my $pos1 = index($data1, "\n"); # $pos1 is 3 my $pos2 = index($data2, "\n"); # $pos2 is 6 my $pos3 = index($data3, "\n"); # $pos3 is 5 # Put together the part before $pos1, # and the part after $pos1. $data1 = substr($data1, 0, $pos1) . substr($data1, $pos1+1 ); # Tell the part of the string that contains # the "\n" to resize itself to nothing, as an lvalue. substr($data2, $pos2, 1) = ''; # Replace the part of the string that contains # the "\n" with three spaces. substr($data3, $pos3, 1, ' '); use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper($data1, $data2, $data3); # prints: # $VAR1 = "abcdef"; # $VAR2 = "foobarbaz"; # $VAR3 = "Albus Percival\nWulfric\nBrian\nDumbledore\n";
Re: Remove Carriage Return
by Skeeve (Parson) on Sep 16, 2005 at 18:25 UTC
    I usually remov all \012 and \015 in order to get the most universal solution for removing line ends.
    s/[\012\015]+//g
    This works okay on all systems I know of.
    Only disadvantage is: You can never tell how many lines you really had as long as you don't know, how a line end is represented in your file.

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print