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

Hi All, I'm just learning Perl at the moment so forgive me if this is a bit simple. I have a few files on a Solaris system which have been transfered from a windows system. when I look at these files in vi every line has a ^M at the end, however when I use perl to look at the file using:
while (<>) { print $_; }
I can't see these characters so that I can process them. Could somone please point me in the right direction? I'm looking at using the ascii value for ^M (0x0D) but I'm not sure if this is the right way or what exactly to do with it if it is. many thanks.

Replies are listed 'Best First'.
Re: can't see ^M to process.
by ikegami (Patriarch) on Jul 19, 2005 at 14:37 UTC

    ^M is vi's representation of carriage returns. When you prit them in Perl, it causes the cursor to move to the beginning of the line. For example, try

    # \015 is carriage return. print("Hello\015you\n");

    You should see "youlo" on the screen. If you redirect the above to a file and open it in vi, you will see "Hello^Myou".

    The following Perl code will remove CRs:

    while (<>) { s/\015//g; print $_; }
Re: can't see ^M to process.
by JediWizard (Deacon) on Jul 19, 2005 at 14:52 UTC

    A shorter version of ikegami's code:

    perl -pi -e 's/\015//g' Filename

    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

      Even shorter
      perl -e 's/\r//g' Filename
        that won't work. I guess you mean:
        perl -pi -e 's/\r//g' Filename
        but, most of the time, only the cr on the end is to be removed:
        perl -pi -e 's/\r$//' Filename

        Paul

Re: can't see ^M to process.
by inman (Curate) on Jul 19, 2005 at 15:33 UTC
    If you still have them on windows you can FTP (or scp) them across using ASCII as the file type. This will automatically translate the carriage returns.

    Optionally check out dos2unix which ships with Solaris.

Re: can't see ^M to process.
by cowboy (Friar) on Jul 19, 2005 at 15:46 UTC
    In vi, or at least vim, you should be able to issue the following command in command mode to remove them.
    :%s/^M//g
    Where ^M is entered by hitting CTRL-V then CTRL-M (you'll see it shows up the same as the offending ^M's) Often systems will also have a dos2unix command. (others above have pointed out how to remove them using perl)

      Even better for vim:

      :set ff=unix

      Ivan Heffner
      Sr. Software Engineer, DAS Lead
      WhitePages.com, Inc.
        Thanks all. :)
Re: can't see ^M to process.
by Anonymous Monk on Jul 19, 2005 at 14:29 UTC
    this is a problem with carriage returns and newlines between windows and unix OS. you need to convert the files <code> tr '\015\' file > file2
      tr -d "\015" < inputfile > outputfile
      will remove ^M