The Haiku/BeOS Tip Server
Tips and tricks for Haiku/BeOS users

Clean up MacOS or Windows text

Contributed by: Chris Herborth and Scot Hacker
This tip is valid for: Both BeOS and Haiku

Bring text files over from MacOS or Windows and they’ll display improperly in BeOS, which uses Unix-style line endings. While there are a variety of tools available on BeWare and BeBits that will take care of this, you can fix these files yourself by using the built-in tr command, for translate. You can get full details on tr with tr --help, but if you just want to fix files fast, here’s the ticket:

To fix DOS/Windows text files, use:

cat original_file | tr -d ‘ ‘ > new_file

To fix Mac text, use:

cat original_file | tr ‘ ‘ ‘ ‘ > new_file

Pete Goodeve ( adds the following alternative method and explanatory text:

If you import a text file from the Microsoft world to use under BeOS (or in Unix for that matter), you are likely to be frustrated by the carriage-return characters that precede every newline in the file (a habit that dates from the days of mechanical teletypes). There is usually at least one ‘control-Z’ marking the end-of-file as well, probably adding to the confusion. These characters may simply show up as rectangles in StyledEdit, or worse, may interfere with compilation.

It is very simple to get rid of these with the following command line, typed in a Terminal:

 tr -d \r\032 <original-text >clean-text 

(‘tr’ “translates’ characters, ‘-d’ means to simply delete rather than actually translate, ‘ ‘ represents the carriage-return character — with the backslash doubled so that is recognized as itself — , and ‘ 32’ — all three digits needed — is the code for control-Z. ‘original-text’ should of course be replaced by the name of the file causing trouble; ‘clean-text’ is where you want to put the useable version.)

By the way, if you have to convert a lot of such files, and you have xicon, a script is available that does the same thing via drag&drop.

Posted in Terminal

(comments are closed).