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

Hey Folks,

Hope the weekend has gone well for everyone. I just had sent to me some C source files, which are part of a program that I need to implement some code for to bring into the 21st century...The crux of my problem lies in the fact that these files were created on a Windows box in VC++ :( and I don't use Windows. When I open the files in VI I get all these pesky ^M chars at the end of each line. I hate them, so what better use can I put Perl to than to parse these files and remove these forsaken control characters. I'm somewhat new to Perl, and have been unable to do these seemingly basic task. Any thoughts greatly appreciated. I've tried various implementations (without luck) such as:
while (<>) { $_ =~ s/\^M//; }
Please help a fellow send these gouldern chars to /dev/null!

Replies are listed 'Best First'.
Re: Windows Ctrl Char
by jasonk (Parson) on Mar 23, 2003 at 21:14 UTC

    You regular expression doesn't specify a control character, it removes a ^ followed by an M. To get rid of the control-M's, use either \cM to specify a control-M, or the special escape character \r, which indicates the same thing. Or do it all on one line like this:

    perl -pi.orig -e 's/\r$//' <filenames>

    You can also do it directly in vi using ':%s/^V^M//g' (where ^V and ^M are literal control characters, hit control-V followed by control-M).


    We're not surrounded, we're in a target-rich environment!
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Windows Ctrl Char
by Zaxo (Archbishop) on Mar 23, 2003 at 21:16 UTC

    This is a standard problem. From the command line perl -pi -e'tr/\015//d' *.c

    After Compline,
    Zaxo

Re: Windows Ctrl Char
by bronto (Priest) on Mar 24, 2003 at 09:28 UTC

    Non-Perl solution:

    :%s/<CTRL+V><CTRL+M>$//

    The prefixing CTRL+V will escape the subsequent CTRL+M, so it won't work as a ENTER key

    Ciao!
    --bronto


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz

      ++bronto

      Note that this is
      :%s/<CTRL+Q><CTRL+M>$//
      If you have sourced $VIMRUNTIME/mswin.vim

      regards,
      tomte


Re: Windows Ctrl Char
by grantm (Parson) on Mar 24, 2003 at 00:45 UTC
    When I open the files in VI I get all these pesky ^M chars

    Are you really using vi? If so, I'd recommend installing vim. One of the (many) features it provides is the ability to transparently read/write files DOS line endings. This might be handy in the event that you need to give the modified source files back.

Re: Windows Ctrl Char
by hacker (Priest) on Mar 24, 2003 at 12:32 UTC
    For Windows-formatted line endings (<CR><LF> pairs)
    perl -pi.$$ -e 's,/\cM//g'

    I recently ran into this on a project, where two "web developers" were working on the same source html files, and I was receiving their updates. One had a Macintosh, and one was using Windows. The Macintosh user's files were all one big long line (so the regex above would not work directly). I had to use the following, to make sure that the ^M was replaced with a \n instead:

    perl -pi.$$ -e 's/\cM/\n/g'

    Or a slightly more-readable version (no LTS):

    perl -pi.$$ -e 's,\cM,\n,g'

    The -pi.$$ there will make a backup copy of each file modified in-place, in case you get the regex wrong and want to recover from it. Using the $$ (PID) there ensures that if you run it multiple times in succession, you'll get a different backup file (unlike -pi.orig, which, when run a second time, would clobber your backup with the modified version).

Re: Windows Ctrl Char
by Mr_Person (Hermit) on Mar 24, 2003 at 17:40 UTC
    I just usually use the flip program, which is included in many Linux distributions. Just a simple flip -u foo.c and you're ready to go. It can also convert back to Windows style if you like.