Converting text files from windows to unix (OSX) and back

paulsomm

yada yada yada ya
If you use BBEdit this isn't a big deal, but I use pico a lot for quick edits of files, and a lot of our developers use Windows to edit the files, so the linebreaks are different, which causes pico (or any Unix editor) to open the file all on one line with no line breaks.

A quick, one line way to convert from windows to unix is at the terminal prompt is to type:

tr '\n' '\r' < /path/to/winfile > /path/to/macfile

I usually create two scripts in my /bin folder called "demac" and "tomac". They actually create a backup copy just in case, and then make the new file the same name as the old. you can copy and past the following into Pico and save them:

demac (to convert from mac to windows):

#!/bin/sh
tr '\r' '\n' < $1 > temp
mv $1 $1.bak
mv temp $1



tomac (to convert from windows to mac):

#!/bin/sh
tr '\n' '\r' < $1 > temp
mv $1 $1.bak
mv temp $1


I then type "demac filename" or "tomac filename" and the original is stored as "filename.bak" just in case
 
Originally posted by testuser
Windows lines end with \r\n
OS X (unix) lines end with \n
Classic Mac OS lines end with \r

Will these scripts result in duplicate line breaks when converting to Mac? (not sure what will happen when converting from Mac to PC)

Or am I mistaken, and can Windows files actually end with just a \r ?

Good question. All I can say is I don't have duplicates.
 
Back
Top