A formatting query

Michael James michael at james.st
Thu Feb 6 09:21:00 EST 2003

On Wed, 5 Feb 2003 04:22 pm, Felix Karpfen wrote:
> Is there a <search-and replace routine|vim plugin> that will convert a
> formatted text file from the Unix to the DOS format?

Couldn't resist posting my little perlscript.
Put in bin and link it 6 ways as dos2unix, unix2dos, mac2unix, etc.

It does what you call it.

#!/usr/bin/perl -pi
# This script will translate text file formats
#  by converting any combination of newline characters

use File::Basename;

my($in, $out) = split( /2/, basename($0) );

$in = newline($in);
$out = newline($out);


sub newline
# expects an OS indicator starting with D, M, or U
# returns the corresponding newline sequence
{       my $os = shift;

        $os =~ /^d/i and { return "\r\n" };     # Newline in DOS = <ret><lf>
        $os =~ /^m/i and { return "\r"   };     # Newline in MacOS = <return>
        $os =~ /^u/i and { return "\n"   };     # Newline in Unix = <linefeed>

        die "What was that you called me? $os doesn't start with D M or U\n";

Michael James				michael at james.st
Network Programmer			voice:	02 6246 5040

More information about the linux mailing list