How to convert newlines in a set of files?

2

I have set of files with either Windows newlines (CRLF) or Unix newlines (LF).

How can I convert all newlines in all files to Mac newlines (CR)?

Thangaraj

Posted 2010-11-16T15:15:41.720

Reputation: 167

I think you need to be more specific about what type of files you have. Are you trying to do this programmatically or just in your OS shell? – None – 2010-11-16T15:16:36.780

Do you want to do it on mac or on unix? – andcoz – 2010-11-16T15:20:54.023

I have hundreds of .c/.h files. I want to convert all those files to mac os format. I am ok with either ways, but my ultimate goal is to convert all those files to mac file format. – Thangaraj – 2010-11-16T15:21:17.470

it would be good if it is in mac. – Thangaraj – 2010-11-16T15:22:24.830

3Search for 'dos2unix' on superuser.com. Voting to migrate. (And by the way, MacOSX systems don't use a different format than unix... you'd have to go back many years in Macintosh operating systems to find a time when they used a different format than MS/DOS and UNIX.) – Ether – 2010-11-16T15:43:58.643

Thanks Ether, Actually I had used dos2unix utility to convert the file format. but still cscope is not able to understand unix file format. that is the reason I post this question. I search in google. but Im not able to get any tools or program to achieve my goal. If you have any idea, please suggest share some script to achieve this. – Thangaraj – 2010-11-16T15:59:13.333

Answers

2

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:

perl -pe 'if (s/\r?\n/\r/g) {$f=1}; if ($f&&eof()) {s/\r\z//}' PCfile.txt >Macfile.txt

or, to convert in place:

perl -pe 'if (s/\r?\n/\r/g) {$f=1}; if ($f&&eof()) {s/\r\z//}' -i convertfile.txt

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.

Gordon Davisson

Posted 2010-11-16T15:15:41.720

Reputation: 28 538

1

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:

dos2unix *.txt
sed -i '' $'s/\r//' *.txt
tr -d '\r'

LF to CRLF:

unix2dos *.txt
recode ../crlf *.txt

LF to CR:

unix2mac *.txt
awk 'BEGIN{ORS="\r"};1'
tr '\n' '\r'

CR to LF:

mac2unix *.txt
awk 'BEGIN{RS="\r"};1'

CRLF or CR to LF:

ruby -e 'print gets(nil).gsub /\r\n?/,"\n"'
ruby -i -pe 'BEGIN{$/="\r"};$_=$_.sub("\n","").sub("\r","\n")' *

CRLF or LF to CR:

ruby -i -ne 'print $_.chomp+"\r"' *.txt

Lri

Posted 2010-11-16T15:15:41.720

Reputation: 34 501