Importing Mac documents to UNIX

Lets consider an example. I have a text file having the following text:

This is written in Microsoft Word for Mac, and saved as a text file.

When imported to UNIX, the contents of the file appear as one long paragraph. (see pic below)

This is because in Mac OS formatting, every new line is marked with ASCII character 15 (carriage return) while in UNIX every new line is marked with a line-feed (ASCII character 12).

So a small script that replaces every occurrence of ‘\15’ with ‘\12’ will do the trick and make the file readable to UNIX.

The tr command replaces every occurrence of a character with another.

tr ’15’ ’12’ <source_file> destination_file

We can make a usable script by typing the following in a text file:

for $x

do

tr ’15’ ’12’ <“$x”> “$x.temp”

mv “$x.temp” “$x”

done

We can execute this script with multiple files as command line inputs. 

I have named my script as “converter.sh”

sh converter.sh file1 file2 …

Advertisements
Tagged , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: