vimdos2unix

dos2unix doesn't convert ^M


I exported results in a text file from a program running on Windows 7, and copied the file on Xubuntu 14.04. In a terminal, I ran dos2unix file.txt, which tells me converting file out_mapqtl.txt to Unix format. However, when I look at the file with less, I still see the Windows end-of-line as ^M, and wc -l returns me "0".

I tried several things described here, but none works. I then opened the file in Vim and did :%s/\r/\r/g as explained there, which worked fine. So any idea why dos2unix didn't work? Would there be a way to avoid opening Vim every time?


Solution

  • \r denotes a carriage return, and on MAC it is used without \n to denote a line break. Are you sure the file is in DOS (\r\n) format and not MAC (\r)?

    If VIM really turns out to be the only thing that'll repair your files, you can also invoke it as:

    vim somefile.txt +"%s/\r/\r/g" +wq

    This will open the file, perform the operation, save it, then quit.

    Can you give us an example of the file, so that we can investigate further?