Saturday, October 11, 2008

Mac Transitions - Fixing Files

In the transition from Linux to Mac, I also ran across a small problem with Mac formatted text files on remote Linux machines. Line endings. I had assumed (and we all know what that means) that the Mac running OS X, being as it's userland roots lie firmly in the BSD camp, would be nice with it's text files and I wouldn't see any nonsense like I do with text files from Windows machines. It's not that they don't work, it's just that they do not look good in my text editor, Vim my favored pager, less and a few other cli programs I use.

Fortunately it is easy to fix the files, either in Vim itself, or with a little script I cooked up to do the job on a bunch of files all at once, here it is...


#!/bin/sh
#
# File name: mac2unix.sh
# Date: 2008/10/11 08:57
# Author: Nigel Houghton
# $Id: $
# Copyright: (c) Sourcefire Inc. 2008
#

if [ "$1" = "" ]
then
echo "Usage: $0 filename1 filename2...."
exit
fi

FILES="$@"

changeit () {
OLDNAME="$file"
NEWNAME="$file.tmp_$$"
tr \\r \\n < $OLDNAME > $NEWNAME
mv $NEWNAME $OLDNAME
}

for file in $FILES
do
changeit
done


Of course, this could be done in several different ways and it could be extended for other systems (Windows for example) that use other line ending schemes. I put this script in my $HOME/bin/ so I can use it from wherever I happen to be in the filesystem.
Add to Technorati Favorites Digg! This

No comments: