Line Breaks

Rodney Sparapani rsparapa at post.its.mcw.edu
Tue Jul 1 17:31:49 CEST 2003


>
>Emcas can be a little slow at this kind of global replacement. For a 
>small file, no problem, of course, but I sometimes have very large 
>files.
>
>Instead, I have a little shell script I routinely use to deal with 
>this kind of problem. The crucial line is the one that uses the "tr" 
>command. As shown below, it is set up to translate Mac end-of-line 
>characters to unix end-of-line characters; you'd have to change it 
>slightly for dos to unix. Mac end-of-line characters are what you 
>will get if, for example, you save an Excel spreadsheet as text in 
>Mac OS X.
>
>Put the script somewhere in your PATH, make it executable. Use the 
>'rehash' command to tell the OS there's something new in the path.
>
>Then type "scriptname filename" at the unix prompt, where filename is 
>the name of one of your .ssc files.
>
>#! /bin/csh -f
>echo Lines before: `wc -l $1`
>setenv tmpfl tmp`echo $$`
># echo temp file name $tmpfl
>mv $1 $tmpfl
>tr '\015' '\n' < $tmpfl > $1
>echo Lines after `wc -l $1`
>## should test for successful return from tr before removing temp file
>rm $tmpfl
>echo Done
>
>I should mention this script has been criticized many times as crude 
>and ugly. That it is. Indeed, the same job can be done just as easily 
>with a perl one-liner.
>
>-Don

Don:

Good point.  And even those platforms that have dos2unix and unix2dos don't 
provide dos2mac, unix2mac, etc.  So, I have the following ksh functions defined:

dos2mac (); { test $# -eq 2 -a "$1" != "$2" && tr -d "\012" < $1 > $2 || echo 
"Usage: dos2mac f1 f2" }

mac2dos (); { test $# -eq 2 -a "$1" != "$2" && { mac2unix $1 $2; unix2dos $2 $2 
} || echo "Usage: mac2dos f1 f2" }

mac2unix (); { test $# -eq 2 -a "$1" != "$2" && tr "\015" "\012" < $1 > $2 || 
echo "Usage: mac2unix f1 f2" }

unix2mac (); { test $# -eq 2 -a "$1" != "$2" && tr "\012" "\015" < $1 > $2 || 
echo "Usage: unix2mac f1 f2" }

The only down-side is that you have to provide two unique file names for tr, but 
a temporary file trick like you proposed could be incorporated.  But, these may 
be overkill since emacs should be able to handle non-native files with 
set-buffer-file-coding-system.  

Rodney Sparapani              Medical College of Wisconsin
Sr. Biostatistician           Patient Care & Outcomes Research
rsparapa at mcw.edu              http://www.mcw.edu/pcor
Was 'Name That Tune' rigged?  WWLD -- What Would Lombardi Do




More information about the ESS-help mailing list