Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dos2unix doesn't convert ^M

Tags:

vim

dos2unix

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?

like image 788
tflutre Avatar asked May 23 '14 11:05

tflutre


People also ask

How do I convert dos2unix?

The simplest way to convert line breaks in a text file is to use the dos2unix tool. The command converts the file without saving it in the original format. If you want to save the original file, add the -b attribute before the file name. Doing so creates a backup file under the same name and the .

How do I use dos2unix recursively?

If you want to automatically run dos2unix on hidden files and folders, you can use find or dos2unix ** **/. * The **/. * will expand only the hidden files and folders, including .

What is M in bash?

^M is a carriage return, and is commonly seen when files are copied from Windows.

How do I run a dos2unix command in Linux?

Sometimes, you will need to move files between windows and unix systems. Window files use the same format as Dos, where the end of line is signified by two characters, Carriage Return or CR or \r followed by Line Feed or LF or \n. Unix files, on the other hand, use only Line Feed (\n).


2 Answers

I know you have gotten this resolved, but I wanted to add a note for reference, based on some testing I've done.

If less is showing ^M, then like Sybren I suspect it is a MAC style ending (\r), not DOS (\r\n). You can determine that easily using cat:

$ cat -e filename

  • Unix endings (\n) show as $
  • MAC endings (\r) show as ^M (less shows these)
  • DOS\Windows endings (\r\n) show as ^M$ (less does not appear to show these)

Use dos2unix to get rid of the DOS (^M$) endings

Use mac2unix to get rid of the MAC (^M) endings - dos2unix won't get rid of these.

I had a file where I had to use dos2unix and mac2unix to get rid of all the non-Unix endings.

like image 117
em_bo Avatar answered Oct 14 '22 11:10

em_bo


\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?

like image 41
dr. Sybren Avatar answered Oct 14 '22 09:10

dr. Sybren