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

Hi,

Once I transferred files from local machine to another machine, one special character (^M) would be appeared at the end of character in each line, as example :

Original ==> abcdefgh
After transferred => abcdefgh^M

Otherwise, the special or aditional character can be found by using vi command, the cat/more command are not helping.

So, could somebody help me ?
Thank you,

Replies are listed 'Best First'.
Re: How to remove "^M" in the files ?
by Zaxo (Archbishop) on Feb 11, 2004 at 06:04 UTC

    To prevent the problem, transfer in 'ASC' mode, assuming ftp. To fix the files you have, perl -pi -e'tr/\015//d' filename

    After Compline,
    Zaxo

Re: How to remove "^M" in the files ?
by Roger (Parson) on Feb 11, 2004 at 06:16 UTC
    There are many ways to remove it -

    In vi:
    :%s/^V^M//g


    Or, run dos2unix:
    dos2unix dosfile.txt unixfile.txt

Re: How to remove "^M" in the files ?
by chimni (Pilgrim) on Feb 11, 2004 at 06:20 UTC

    you could also use the command dos2ux
    dos2ux myfile > newfile
    newfile will be in the proper format
    of course s/// and tr/// will also do
    hth
Re: How to remove "^M" in the files ?
by Abigail-II (Bishop) on Feb 11, 2004 at 10:27 UTC
    In stead of focussing how to remove the ^M, I'd look into how the ^M got there in the first place, and eliminate that. One way of ^M appearing is if you transfer files from a Unix platform to a DOS (or Windows) platform using FTP in ASCII mode. This is usually what you want though. Transferring in binary mode makes that FTP leaves your file as is.

    What also might happen is that you transfer file from Windows to Unix using FTP in binary mode, but you never noticed the ^M on Windows, because, hey, that's the way it works out there. Then the solution is to transfer files in FTP ASCII mode.

    Abigail

Re: How to remove "^M" in the files ?
by BUU (Prior) on Feb 11, 2004 at 06:08 UTC
    \r\n is windows line ending, linux line ending is \n, \r shows as ^M in vi.

      No cookie for you. "\n" is the Unix line ending, which Linux-based systems just happen to follow. Also, the '^M' tends to be the common way of showing "\r" in most Unix applications, not just vi.

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

        It *is* the linux line ending though. It's *also* the unix one, but that doesn't mean it's not the linux one. And yes, not just vi, laziness, etc.