DISQUS

DISQUS Hello! Tech-Recipes is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Tech-Recipes

Cookbook of Tech Tutorials
Jump to original thread »
Author

Remove ^M characters at end of lines in vi | UNIX | Tech-Recipes

Started by qdideas · 9 months ago

No excerpt available. Jump to website »

31 comments

  • Another way to do this is with the dos2unix utility. You can also use unix2dos to add in the extra ^M (carriage-return) characters to a text file so it's readable under DOS.
  • Where foo is the file that has the control M's

    cat foo | col -b > foo2
  • Thanks it is working fine
  • this works ! thanks.
  • type the following command:

    :%s/<control-v><control-m>//g

    ^M is the result of a control V, control M at the end of the line. if you actually hit control-v then control-m, "^M" will appear and will get rid of the character at the end of the line. hope this helps someone!!!
  • You are a genius
    roy@roy.com
  • col -bx <dosfile> newfile
    does the biz. too :)
  • i want to include into .profile the possibility to dont give a user the prompt
  • The information about this issue was exactly what I was looking for.

    Right On!
  • Hi this was so useful it help me a lot and our compnpay H.L.L India
  • ...I also needed this desperately!! Thanks for posting it!
  • the dos2unix command does this too - though it may be deprecated.
  • ok so no flames :oops:
  • :D Perfect.....just what I needed ! Many Thanks !
  • Thanks to all techy guys out there who posted solutions for this.
  • <ul id="quote"><h6>Richard wrote:</h6>:D Perfect.....just what I needed ! Many Thanks !</ul>
  • Hi,

    THere is lot of non-continous blank lines in my shell. I want to find the blank line and I want to remove it. What is the command for that?

    Sasi, India
  • If you want to remove all blank lines in your file, search for empty lines and replace them with nothing.. since ^ means beginning of line and $ means end of line, ^$ means a blank line, so

    :%s/^$//g


    will find blank lines and make them disapear.
  • All- I need to clean a bunch of files in UNIX that contain the ^M characters.

    While I can do each one individually by :%s/^M//g I would like to know if there is an easy command line execution to serarch for any files containing the errors and correct them.

    I tried this for all *.sql files in a given directory, but it didn't work:

    for name in `ls *.sql` ; do sed 's/^M//' $name > ${name/.sql/N.sql} ; mv ${name/.sql/N.sql} $name ; done

    If anyone has a suggestion or knows a simple script that can be run please let me know.

    Thanks
  • You could try using sed, the stream editor - it works on similar principles to vi's replace tool, but you can pipe into and out of it.

    So, to change one file:
    cat fileName | sed s/<ctrl-v><ctrl-m>//g >tmp && mv tmp fileName

    Do NOT try to read from and write to the same file in one pipe, or you'll blank the file

    To change all files in a directory with the .sql ending, run the following script whilst inside that directory:
    #/bin/ksh
    for fileName in $(ls *.sql); do
    cat $fileName | sed s/^M//g >tmp && mv tmp $fileName
    done

    (bear in mind to enter ^M you need to enter <ctrl-v><ctrl-m> as above.

    Hope that helps people
    -phobiandarkmoon
  • type the following command in vi

    :%s/.$//
  • Try perl -pi -e'tr/\015//d' <filename>
  • yes the ":%s/^ v ^M//g" is working... thank you very much...
  • this solution is not working for ubuntu..:(
  • thank you for the nice tips. I have plenty of ctrl+M symbols in the dump txt files from dol.
  • great info.. thanks
  • another way to get rid of those ^M's
    :%s/\r//g
    btw other systems then *nix are treating newlines differently :P
  • :%s/\r/\r/g
  • use $ to replace only at the end
    :%s@^M$@@g
  • Excellent
  • thanks it works well

Add New Comment

Returning? Login