Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

vim - dos2unix doesn't convert ^M - Stack Overflow

dos2unix doesn't convert ^M characters

Uploaded by

gblackwe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

vim - dos2unix doesn't convert ^M - Stack Overflow

dos2unix doesn't convert ^M characters

Uploaded by

gblackwe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

vim - dos2unix doesn't convert ^M - Stack Overflow https://stackoverflow.

com/questions/23828554/dos2unix-doesnt-convert-m

Just browsing Stack Overflow? Help us improve your experience. Sign up for research

dos2unix doesn't convert ^M


Asked 10 years, 6 months ago Modified 4 years, 10 months ago Viewed 15k times

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

vim dos2unix

Share Improve this question edited May 23, 2017 at 11:54 asked May 23, 2014 at 11:54
Follow Community Bot tflutre
1 1 3,526 9 40 56

1 A bit late here... But I wrote a small program that makes life easier than dos2unix when you're not
sure about the input format, or when input formats are intermingled : github.com/mdolidon/
endlines – Mathias Dolidon Jul 12, 2015 at 9:45

@MathiasDolidon Thank you, Mathias! – isomorphismes May 12, 2018 at 1:57

Your question can be rephrased as: Why doesn't dos2unix modify a text file? ~ * ~ Here is a question
asking the opposite: Why does dos2unix modify a binary file? ~ * ~ A simple answer to both
questions is: because dos2unix isn't foolproof. – Henke - Нава́льный П с м Oct 22, 2022 at 16:31

4 Answers Sorted by: Highest score (default)

1 of 3 4/12/2024, 7:16 pm
vim - dos2unix doesn't convert ^M - Stack Overflow https://stackoverflow.com/questions/23828554/dos2unix-doesnt-convert-m

I know you have gotten this resolved, but I wanted to add a note for reference, based on
some testing I've done.
21
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.

Share Improve this answer Follow answered Mar 21, 2017 at 16:07
em_bo
673 7 15

thanks for this answer, I was stumped running dos2unix, didn't realise the file was using the old mac
line endings somehow. – Pellet May 24, 2017 at 0:08

IMO, should be actually the answer. – Serge Roussak Feb 29 at 13:12

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

2 of 3 4/12/2024, 7:16 pm
vim - dos2unix doesn't convert ^M - Stack Overflow https://stackoverflow.com/questions/23828554/dos2unix-doesnt-convert-m

sorry for the delay of my answer. The file was produced on Windows 7, thus I assume it is in the
Share Improve this answer Follow answered May 23, 2014 at 11:59
DOS format. I'm not even sure the program runs on Mac. By the way, the program is called
dr. Sybren
MapQTL. Anyway, my problem is now solved, thanks. – tflutre May 27, 2014 at 9:36
855 5 20
Thanks for letting us know your problem has been solved, and accepting my solution as the answer.
– dr. Sybren Jun 2, 2014 at 12:43

hi, what does " %s/\r/\r/g" do ? it looks like sed expression . replace '\r' with "\r"? – nathan Dec 8,
2016 at 0:26

1 yup, that's what it does. The % in front applies the expression to all lines in the file. Due to the line-
ending-handling magic of VIM, this seemingly useless expression apprarently does something.
– dr. Sybren Dec 11, 2016 at 4:05

Try this:

2 tr -d '\r' < file

Share Improve this answer Follow answered May 23, 2014 at 12:00
Mark Setchell
206k 32 300 476

I have used Notepad++ feature: Edit>EOL Conversions>Unix(LF).

Now export this file to the Unix machine using pscp.exe.


0
Let me know if that worked for you.

Share Improve this answer edited Jan 24, 2020 at 10:54 answered Jan 24, 2020 at 7:18
Follow Yogi Shadow79art
635 1 8 22 1 1

3 of 3 4/12/2024, 7:16 pm

You might also like