Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to export a string for a bash script. The terminal is naturally very picky when it comes down to the "\n\r" vs "\n", Windows vs. Linux, new line issue.

How can I teach Mathematica to use "\n" rather than "\n\r" as the line separator when exporting text (in Windows)?

I found a couple of posts regards the same issue, but they were concerned with importing files. For example, here. None of the suggested solutions worked for my case.

I played with CharacterEncoding and "LineSeparators" but nothing worked.

EDIT: Great thanks to Mark Adler for giving me the right clue. His suggested solution WriteString[f, "\n"] did not work because "\n" is interpreted by Mathematica as string not as end-of-line.

However, his suggested DOSTextFormat -> False option also works as option for Export[].

I got the desired result using Export[filename,script,"Text",DOSTextFormat -> False].

share|improve this question

3 Answers 3

You can use WriteString[] which does not append a new line, and then write the new line character yourself. E.g. WriteString[f, "\n"].

Also you can try DOSTextFormat -> False as an OpenWrite[] option. I don't have a Windows system to try it out on.

share|improve this answer
up vote 0 down vote accepted

Great thanks to Mark Adler for giving me the right clue. His suggested solution WriteString[f, "\n"] did not work because "\n" is interpreted by Mathematica as string not as end-of-line.

However, his suggested DOSTextFormat -> False option also works as option for Export[].

I got the desired result using Export[filename,script,"Text",DOSTextFormat -> False].

share|improve this answer

This did not work for me because I wanted to create a Windows CSV file from a Macintosh. I tried DOSTextFormat->True but it did nothing. I found that you have the most control using the "Table" format because you can set the field and line separators. For CSV windows files:

Export[fileToWrite, avgdata, "Table", "FieldSeparators" -> ",", "LineSeparators" -> "\r\n"]

For CSV Mac/Unix files:

Export[fileToWrite, avgdata, "Table", "FieldSeparators" -> ",", "LineSeparators" -> "\n"]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.