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

Monks, I am attempting to remove all ^M characters within a file. What would the easiest solution to this. Thanks--

Replies are listed 'Best First'.
Re: Removing Control Characters
by merlyn (Sage) on Feb 22, 2001 at 19:41 UTC
vi Rooolzzz
by grinder (Bishop) on Feb 22, 2001 at 19:29 UTC
    :%s/^V^M
    oh, you meant perl...
    perl -ibak -pe 's/\r//' README.DOS
    Update: I actually wanted the title to be vi roolz, but it turns out you can't have html tags in titles, which meant that the title was munged. Fixed now.

      OUTSTANDING!!!

      I love PM I really do, how did I not know about -i, what a cool and goovey flag. It is times like this when I just boggle at perl, how can one language be so versitile.

      Ignore my node later raj8 this is a far better way of doing what you want. Although I would recomend the $ in the regexp:

      perl -ibak -pe 's/\r$//' README.DOS

      --

      Zigster

      Although most of the time you want to use:

      :%s/^V^M//g

      --Jeroen/Asmodai

Re: Removing Control Characters
by coreolyn (Parson) on Feb 22, 2001 at 21:42 UTC

    I'm probably going to get downvoted for this but many unix platforms have a dos2unix utility.

    dos2unix ./dosfile > ./unixfile

    coreolyn
      You didn't get downgraded on that. I liked the quick and simple option. I liked the perl options too, but yours was easily done on a Solaris box. Thanks for the added wisdom. raj8
Re (tilly) 1: Removing Control Characters
by tilly (Archbishop) on Feb 22, 2001 at 23:13 UTC
    If you have some files from Macs as well as DOS:
    perl -pi.bak -e 's/\r\n?/\n/g' file1 file2
    The point being that this will correctly convert both \r\n and \r line endings.
Re: Removing Control Characters
by zigster (Hermit) on Feb 22, 2001 at 19:30 UTC
    perl -p -e "s/\r$//" (FileName)
    this will print the file to std out DO NOT be tempted to redirected back to the original file as you will truncate it. Something like
    perl -p -e "s/\r$//" (FileName) > op;mv op (FileName)
    Will strip the ^M's and put the file back where it started.
    HTH
    --

    Zigster
      zigster writes:

      DO NOT be tempted to redirected back to the original file as you will truncate it.

      Better yet, use Perl's in place file editing feature:

      perl -p -i -e 's/\r$//' (filename)

      Plus, if you use

      -i.foo
      a backup copy of filename before the changes will be stored in "filename.foo"

      Peace,
      -McD

Re: Removing Control Characters
by unixwzrd (Beadle) on Feb 22, 2001 at 19:33 UTC
    Try using the regex:
    s/^M//g
    That should do the trick for you. I hope I don't have to mention that the "^" is not a caret which would simply result in anchoring an "M" to the beginning of the line, I also leave out the "$" since they might be in other places in the file. I try not to make assumptions... (It's early and I'm just getting coffee...)

    Update: Pet peeve - Users (developers) who use Samba mounts and "notepad" to edit source and data files to be used by the Sun box...

    Mike - mps@discomsys.com

    "The two most common elements in the universe are hydrogen... and stupidity."
    Harlan Ellison
Re: Removing Control Characters
by converter (Priest) on Feb 22, 2001 at 20:09 UTC

    Assuming you're working from the command line:

    perl -015 -pi.bak -e "chomp" filename