On Thu, Oct 28, 2010 at 9:28 PM, Jeff Peng <pen...@nsbeta.info> wrote:
> You get the files from windows to un*x?
> try the command 'dos2unix'.

^G is a bell character (\a). That's not a platform issue, AFAIK.
Anyway, in response to this thread I wrote Perl scripts that seem to
work as replacements for dos2unix and unix2dos.

dos2unix.pl:

#!/usr/bin/env perl

use strict;
use warnings;

use File::Copy "cp";
use File::Copy "mv";
use File::Temp qw{ tempfile };

if(@ARGV == 0)
{
    print STDERR "Usage: dos2unix.pl file...";
    exit 1;
}

for my $in_fn (@ARGV)
{
    open my $in_fh, "<", $in_fn or die "Failed to open '$in_fn': $!";
    my($out_fh, $out_fn) = tempfile() or
            die "Failed to open temporary file: $!";

    while(my $line = <$in_fh>)
    {
        $line =~ s/\cM//g;
        print { $out_fh } $line;
    }

    close $in_fh or die "Failed to close '$in_fn': $!";
    close $out_fh or die "Failed to close temporary file '$out_fn': $!";

    my $backup = "$in_fn.orig";

    die "Backup file '$backup' already exists! Aborting..." if -e $backup;

    cp $in_fn, "$backup" or die "Failed to backup '$in_fn': $!";
    mv $out_fn, $in_fn or die "Failed to replace '$in_fn': $!";
    unlink "$backup" or
            die "Failed to unlink backup file '$backup': $!";
}

__END__

unix2dos.pl:

#!/usr/bin/env perl

use strict;
use warnings;

use File::Copy "cp";
use File::Copy "mv";
use File::Temp qw{ tempfile };

if(@ARGV == 0)
{
    print STDERR "Usage: unix2dos.pl file...";
    exit 1;
}

for my $in_fn (@ARGV)
{
    open my $in_fh, "<", $in_fn or die "Failed to open '$in_fn': $!";
    my($out_fh, $out_fn) = tempfile() or
            die "Failed to open temporary file: $!";

    while(my $line = <$in_fh>)
    {
        $line =~ s/\cJ/\cM\cJ/g;
        print { $out_fh } $line;
    }

    close $in_fh or die "Failed to close '$in_fn': $!";
    close $out_fh or die "Failed to close temporary file '$out_fn': $!";

    my $backup = "$in_fn.orig";

    die "Backup file '$backup' already exists! Aborting..." if -e $backup;

    cp $in_fn, "$backup" or die "Failed to backup '$in_fn': $!";
    mv $out_fn, $in_fn or die "Failed to replace '$in_fn': $!";
    unlink "$backup" or
            die "Failed to unlink backup file '$backup': $!";
}

__END__

I was going to suggest dos2unix to solve half of this problem (hadn't
seen ^G before). In searching for a Perl driven solution I installed
the 'tofrodos' package on my new Debian 'squeeze' server to try to
integrate it with Perl. After installing it I couldn't find any of the
unix2dos/dos2unix variants on my system. :( I guess I ended up writing
simplified versions in Perl. At least they seem to work so I added
them to my PATH. :)

Thank you, Shlomi Fish, for your post. :) That lead me to learn a
little something new (as usual).

-- 
Brandon McCaig <bamcc...@gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamcc...@castopulence.org>

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to