Having Mathematica replace DOS line breaks with UNIX in files
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.
You may try this:
s = Import["Silly.tex", "Text"];
s = StringReplace[s, "\r\n" -> "\n"];
Export["Silly_new.tex", s, "Text"]