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

Unable to remove ^M in the file

while rewrite the file content using perl script, it is added with ^M at end of each line.

Replies are listed 'Best First'.
Re: Problem in removing ^M character in imported view
by cdarke (Prior) on Jul 22, 2009 at 07:21 UTC
    ^M is the "carriage return" character, represented by "\r". It is added before the newline character ("\n") typically on Microsoft Windows.

    There are several ways around it. Make sure you save the file as a UNIX file format, not Windows (sometimes called "DOS").
    You can remove it in Perl with :
    $lines[$i] =~ s/\r//g;

    or:
    local $/ = "\r\n"; chomp @lines;
    but you will have to add the "\n" when writing the line out.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Problem in removing ^M character in imported view
by ELISHEVA (Prior) on Jul 22, 2009 at 12:47 UTC

    Welcome to Perl Monks!

    Your post here and Re^2: Problem in removing ^M character in imported view are currently very hard to read. That is limiting our ability to help you. You can help us help you by taking the time to add formatting to your post.

    To get your post into edit mode, just click on the title of the post. You can use plain old HTML to format your posts. We also have a Perl Monks specific tag <code> (or altenatively <c>) that you can use to mark the start and end anything that is code. This will protect the layout and spacing of your code sample. As I am sure you realise, unformatted code is very hard to read. For more information on how to use <code> tags and HTML in your post see Markup in the Monastery.

    If you are unsure of anything in the above help document, feel free to ask for help in the chatterbox found on the right side of your Perl monks page.

    Best, beth

Re: Problem in removing ^M character in imported view
by vinoth.ree (Monsignor) on Jul 22, 2009 at 06:35 UTC