I have set of files with either Windows newlines (
CRLF) or Unix newlines (
How can I convert all newlines in all files to Mac newlines (
migrated from stackoverflow.com Nov 17 '10 at 0:53
This question came from our site for professional and enthusiast programmers.
The question doesn't explicitly say, but I assume you mean text files, and need to convert the line delimiter format? OS X doesn't ship with command-line file converter tools, you have to build them yourself. Perl is good for Q&D utilities like this:
or, to convert in place:
Note: this script is a little more complicated than it probably needs to be, because it's written to work on on both PC-format (CRLF line terminators) and unix files (LF terminators), and leave files that're already in the old traditional MacOS format (CR separators between lines) alone. Also, the PC and unix formats put a terminator after the last line, while Mac format doesn't (it uses line separators, not terminators), so this script detects when it's actually translating, and removes the last delimiter.
CR line endings were mostly used in Mac OS 9 and earlier. OS X uses LF line endings in most places.
Some of the methods below add a newline to the end of files that don't already end with a newline. Some don't work on Windows or if the default line endings are not LF.
CRLF to LF:
LF to CRLF:
LF to CR:
CR to LF:
CRLF or CR to LF:
CRLF or LF to CR: