A client recently supplied some old data from an archive cd produced several years back

The data had been edited on a Mac system prior to OSX

Commodore 64There are convenient Gnu / Linux tools for converting CR+LF  to Line Feed (U+000A), however lone CR (Mac OS 9 and Commodore 8 bit) had me running for emacs and sed.

Ctrl-X and Equals (=) in Emacs will tell you about the document generally, your position, and the hex for the character under the cursor.

x0d or more correctly x0D is the lone CR

sed 's/\x0D/\n/g' < loneHatM.txt > fixed.txt

…is useful.

However if you already have dos2unix and tofrodos installed on Debian, then these two commands, in the sequence shown, will work also:

  1. dos2unix -l oldmac.data
  2. fromdos -a oldmac.data

Quoting directly from the dos2unix manpage about -l switch:

-l –newline
Add additional newline.  Only DOS line endings are changed to two Unix line endings.  In Mac mode only Mac line endings are changed to two              Unix line endings.

Quoting directly from the fromdos manpage about -a switch:

-a     Always convert. If converting from DOS to Unix, this option will cause the program to remove ALL  carriage  returns.  The  default  is  to
remove  carriage  returns  only if they are followed by line feeds.  If converting from Unix to DOS, this option will cause the program to
convert ALL linefeeds to carriage return pairs. The default is to convert linefeeds only if they are not already preceded  by  a  carriage
return.

On this occasion I was lucky in that there were some lookup tables in addition to the main load to MySQL, and these tables were small enough to open in an editor (emacs)

For larger files either take the first few hundred characters (use cut command) or inspect with a hex editor without opening the entire file.

Max OSX now uses the Unix / Linux convention, so thankfully these conversions are now rare.

Postscript: Turns out that dos2unix utils has a convenient mac2unix command, which does exactly what I require for this client work.

( Given the mac change OS9 to OSX, i can see the need to create a backup before you try out mac2unix the first time )

( The image of the commodore 64 is courtesy of Wikipedia and is located at this page )

Advertisements