# Having Mathematica replace DOS line breaks with UNIX in files

Say I have a file named "Silly.tex" which has \r\n line breaks. Is there a way to get Mathematica to convert it to only have \n line breaks?

You can find such a file with DOS line breaks at: https://math.byu.edu/~pace/Silly.tex

There are two "text" formats that work with Import/Export, namely "String" and "Text".

"String" will read/write the bytes in the file as they are. It does not try to interpret those bytes as text at all. It just puts them into a String data structure as they are.

"Text" will interpret the bytes as text. What does that mean? Well, a "text file" may be many different things. There are several different conventions about how text can be represented by bytes (numbers) in a file. The most important thing is the character encoding. It can be set for "Text" import. It can be ASCII, UTF-8 or something else. Then there's a convention about line endings. "Text" import interprets all of these, while "String" import does not.

As documented, "Text" import will replace all common line ending markers by \n on import (i.e. you'll get an \n in the string, no matter what was in the file). On export, the ending convention of the operating system is used.

What this means is that if you are on macOS or Linux, you need to import as "Text", after setting the CharacterEncoding if necessary (in practice: if you have any accented or non-Latin characters), then just re-export as "Text" again. The line endings will be Unix style.

If you are on Windows, this will not work, as the exported result will always use DOS style line endings.

When you can do then is to import as "String", do the string replace that Henrik suggested, StringReplace[s, "\r\n" -> "\n"], then re-export as "String". This method should work on all platforms.

• It didn't work the first time I tried it. But then I realized that "\n\r" should instead be "\r\n"! With that additional change, it worked. – Pace Nielsen Nov 27 '18 at 23:13

You may try this:

s = Import["Silly.tex", "Text"];
s = StringReplace[s, "\n\r" -> "\n"];
Export["Silly_new.tex", s, "Text"]

• @PaceNielsen Did it work? – Henrik Schumacher Nov 27 '18 at 9:57
• When I replaced .tex with something nonsensical like .txx it also doesn't work, saying "Export::infer: Cannot infer format of file M4N13R-511.txx." – Pace Nielsen Nov 27 '18 at 15:51
• @PaceNielsen Oops. I forgot to feed the actual string into the Export command. Please try again. (Things like this happen easily if the poster of a question does not include a minimal example for testing...) – Henrik Schumacher Nov 27 '18 at 15:56
• It still isn't working. (The line breaks are not changed.) I've now included a link to such a file with DOS line breaks, if that helps you. – Pace Nielsen Nov 27 '18 at 21:34