Technology TidBits

Answers to various technical questions on php programming, mysql, linux, and many more categories.



How can I remove DOS linebreaks (^M) from my files?:
Last updated: 07/03/2009
Add comment
Votes: 5
Comments: 0

Oh, the dreaded ^M - these usually occur when uploading files from a windows system, using ascii mode.  Some Windows editors may add the character to the end of each line as well during editing.

Sometimes, it's hard to even find them.  One pretty foolproof way on a Linux / Unix system is to use cat:

cat -e [filename]

This will show you lines with a $ at the end, and ^M's should show up, if they are there.  Here are a few options to remove them:

Method 1:

    In the vi editor, do the following:

    :.,$s/(ctrl-v)(ctrl-m)//g (enter)

    After typing the (ctrl-v)(ctrl-m) combination, you should see a "^M" in the line (note, the ^M is NOT just a '^' and an 'M', it's a special control character. If you try to reproduce it without the control sequence I describe here, you'll really mess up your file.

    By the way, if you do screw something up, just type:

    :q!

    which will exit the editor without saving, and you can start fresh and try again.

Method 2: Using sed

    From your Linux shell prompt, do the following:

    sed -e 's/(cntrl-v)(cntrl-m)//g' [filename] > [new filename]

    This creates a new file with the ^M's removed

Method 3:

    If your system has the 'dos2unix' utility, you can do this:

    dos2unix [filename]

    This has the advantage of replacing the original file, so you don't need to move anything after running this command.

Other questions in this category:
How do I set Tab stops in VI?
How can I turn on line numbering in Vi?
How can I jump to a line in the vi editor?
How can I remove leading whitespace in all lines in a file using vi?
How can I delete a large chunk of text using vi?
How can I undo and redo changes in vi?



Powered by KnowledgebasePublisher 1.1
Banner
Need a developer for your next project? Find a freelancer here.
Content provided by Roberts WebForge, Inc.