[OS X TeX] verbatim problems

Anthony Morton amorton at fastmail.fm
Thu Jul 6 06:30:08 CEST 2006


> I actually have this as a script called mac2unix, created analogously 
> to what David described. Its content is a bit shorter -
>
> #!/bin/csh
> tr "\015" "\012" < $1 > $2
>
> That's all - you'd invoke this with "mac2unix INTEXTFILE  OUTTEXTFILE" 
> from the Terminal command line.

I have a similar set of hand-rolled scripts for this sort of thing: 
here's one that does much the same as the above, but handles multiple 
files at once and also optionally replaces the original file.

--------
#!/bin/sh
# Shell script to convert files named by arguments from Mac to Unix 
line breaks
# Default is to place in new .utxt files; specify -f to replace 
originals

delete_orig=no
if [ "$1" = -f ]
then
         delete_orig=yes
         shift
fi

for i in $*
do
         if [ -e "$i.utxt" ]
         then
                 echo mac2unix: file $i.utxt exists, skipping
         else
                 tr '\r' '\n' < "$i" > "$i.utxt"
                 if [ $delete_orig = yes ]
                 then
                         mv "$i.utxt" "$i"
                 fi
         fi
done
--------

To use this, you type 'mac2unix file1 file2 file3....' and you get new 
files called file1.utxt, file2.utxt, etc which you can then rename.  
Alternatively type 'mac2unix -f file1 file2 file3....' and the new 
files replace the original ones.

To go the other way, just exchange the '\r' and the '\n' in the 'tr' 
command line straight after 'else'.

And to convert from DOS to Unix line endings, replace the 'tr' command 
line with

	tr -d '\r' < "$i" > "$i.utxt"

which deletes the extra CR character from each line ending.

As with any Unix shell script, it's a really good idea to put 
substituted arguments in double quotes, hence "$i" and so forth.  
Otherwise you get in trouble when dealing with files whose names 
contain space characters.

> OK - before sending this off I just checked if there's an _even_ more 
> "user-friendly" solution because I recall that some people seem to 
> dislike the command line... And indeed, there is a bona fide 
> application that does this:
> LineBreak, from http://www.trancesoftware.com/software/linebreak/

There's also Dave Grunenberg's WordService, a free download from 
<http://www.grunenberg.net>.  It installs lots of neat stuff in the 
Services menu, so all you have to do is select some text and select the 
appropriate function: change line endings, count words, convert case, 
etc.  Though it's more for operating on small chunks of text than 
entire files, I guess, and Services do only work in Cocoa applications. 
  Highly recommended nonetheless.

Tony M.

------------------------- Info --------------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
          & FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Archive: http://tug.org/pipermail/macostex-archives/




More information about the macostex-archives mailing list