[techtalk] vi technique for dos2unix

Sean McAfee mcafee at umich.edu
Wed May 3 13:50:41 EST 2000


"Christian MacAuley" <jellhead at jellspace.net> wrote:
>I wrote this script to get rid of the ghastly carriage returns in my DOS
>files on my FreeBSD system.

>=======================
>#!/usr/local/bin/tsh
>
>if test $# -eq 0
>  then echo "You didn't enter a filename."
>elif test -f "$1"
>  then cat $1 | tr -d "\015" > $1
>  echo "The carriage returns are all gone."
>else
>  echo "That file doesn't exist."
>fi
>======================

Allow me to suggest a much shorter alternative:

perl -i -pe 'tr/\015//d' file1 file2 ...

The "-i" is for "inplace edit", one of Perl's most useful features, IMHO.  If
you're paranoid, you can save the original files with a backup extension by
specifying it after the -i:

perl -i.bak -pe 'tr/\015//d' file1 file2 ...

This will save the original file1 as file1.bak, file2 as file2.bak, etc.

Recently I found myself in the strange position of needing to convert a
local file to DOS format so I could transmit it to a printer that only
grokked the DOS line format.  Perl came to the rescue again:

perl -i -pe 's/$/\r/' myfile

-- 
Sean McAfee | GCS d->-- s+++: a27 C++ US+++ P+++$ L++ E- W+ N++ |
            | K w--- O? M- V-- PS+ PE Y+ PGP?>++ t+() 5++ X R+  | mcafee@
            | tv+ b++ DI++ D+ G e++ h r---* y+>++               | umich.edu





More information about the Techtalk mailing list