DISQUS

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

  • bofh468 · 6 years ago
    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.
  • Michilimackinac · 5 years ago
    Where foo is the file that has the control M's

    cat foo | col -b > foo2
  • Reetesh · 1 year ago
    Thanks it is working fine
  • a · 8 months ago
    this works ! thanks.
  • ucsbliu · 2 months ago
    Thank you, original post did not work for me, but this did.
  • nxnwaus · 5 years ago
    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!!!
  • roy · 7 months ago
    You are a genius
    roy@roy.com
  • SubZero · 5 years ago
    col -bx <dosfile> newfile
    does the biz. too :)
  • Anonymous · 5 years ago
    i want to include into .profile the possibility to dont give a user the prompt
  • BigJoe · 4 years ago
    The information about this issue was exactly what I was looking for.

    Right On!
  • jibin.thomas@gmail.com · 4 years ago
    Hi this was so useful it help me a lot and our compnpay H.L.L India
  • Anonymous · 4 years ago
    ...I also needed this desperately!! Thanks for posting it!
  • johnny · 4 years ago
    the dos2unix command does this too - though it may be deprecated.
  • johnny · 4 years ago
    ok so no flames :oops:
  • Richard · 4 years ago
    :D Perfect.....just what I needed ! Many Thanks !
  • Prasad · 4 years ago
    Thanks to all techy guys out there who posted solutions for this.
  • Anonymous · 4 years ago
    <ul id="quote"><h6>Richard wrote:</h6>:D Perfect.....just what I needed ! Many Thanks !</ul>
  • sasi · 4 years ago
    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
  • qmchenry · 4 years ago
    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.
  • Anonymous · 3 years ago
    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
  • Phobian · 1 year ago
    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
  • Maxlen · 1 year ago
    type the following command in vi

    :%s/.$//
  • Yo Whirrd up · 1 year ago
    Try perl -pi -e'tr/\015//d' <filename>
  • SOM · 5 months ago
    Great dude .. this works
  • pradeep · 1 year ago
    yes the ":%s/^ v ^M//g" is working... thank you very much...
  • soma · 1 year ago
    this solution is not working for ubuntu..:(
  • thanks · 11 months ago
    thank you for the nice tips. I have plenty of ctrl+M symbols in the dump txt files from dol.
  • Neo · 10 months ago
    great info.. thanks
  • 3pe · 10 months ago
    another way to get rid of those ^M's
    :%s/\r//g
    btw other systems then *nix are treating newlines differently :P
  • Ashish · 8 months ago
    :%s/\r/\r/g
  • xx · 7 months ago
    use $ to replace only at the end
    :%s@^M$@@g
  • sam · 6 months ago
    Excellent
  • S · 6 months ago
    thanks it works well
  • Ciprian · 5 months ago
    Lovely! I completely forgot about dos2unix and sincerely I like to use vim better if I can :)
  • Vishal · 5 months ago
    I am trying ti get the diff of a file into a temp.txt file.
    When I do :%s/^M//g in my file, it says "Pattern not Found: ^M".
    But when I see the temp.txt file for the diff it shows ^M on all the lines.
    Help me out for this, I dont want ^M in temp.txt file
  • raod · 5 months ago
    Thank you very much...for me saved lot of time.
  • SnowLeopard · 3 months ago
    Thanks! :D
  • awais · 3 months ago
    hi,
    grt man. it worked for me. keep up the good work.

    br
  • Dmitriy Golub · 2 months ago
    Nice, thank you!
  • mhannesy · 2 months ago
    Just curious, :%s/\r//g works but :%s/\r\n/\n/g does not, why is that?
  • mhannesy · 1 month ago
    Ok, I found out why I ended up with the NUL control characters instead. From :help insert:

    "If you enter a value of 10, it will end up in the file as a 0. The 10 is a
    <NL>, which is used internally to represent the <Nul> character. When writing
    the buffer to a file, the <NL> character is translated into <Nul>. The <NL>
    character is written at the end of each line. Thus if you want to insert a
    <NL> character in a file you will have to make a line break."
  • Brett · 1 month ago
    You Sir are a genius. I award you five internets!
  • Hedi · 1 month ago
    You could try :
    echo file | sed 's/\r//g'
  • Name · 2 weeks ago
    I am brand new to Vim. I am trying to remove ^M and replace it with a tab (actually replace it with spaces). I can't seem to get this to work. I've done the following things and it isn't working. Any advice?

    :set expandtab
    :%s/^M/<Tab>/g

    All it is doing is putting "<Tab>" with the "^M" used to be. Any way to actually make the spaces appear? Thanks for your help.
  • Name · 2 weeks ago
    Nevermind! I figured it out. I simply had to push "control + the tab key" and that resulted in what I needed. Thanks.
  • vijaycgowda · 1 week ago
    hi,
    Thanks for the info.. I need to move the lines to the top after the ctrlM charecter. Is this possible ??