Operating System - HP-UX
1753404 Members
7242 Online
108793 Solutions
New Discussion юеВ

howto convert text files unix2dos using perl script ?

 
SOLVED
Go to solution
'chris'
Super Advisor

howto convert text files unix2dos using perl script ?

hi

I transfer text files via ftp from LINUX to the WINDOWS ftp server using this perlscript:
......................................................................................................
my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3);
$ftp or die "$server: cannot connect: $@";
$ftp->login ($user,$passwd) or
die "$_: Could not login: " . $ftp->message;
# Put file
$ftp->ascii;
$ftp->put ($file) or
die "$server: cannot put $file: " . $ftp->message;
$ftp->quit;
......................................................................................................

but files after transfer are corrupt.

howto convert using perl the unix format file to dos before ftp the files ?

regards
chris

5 REPLIES 5
Abdul Rahiman
Esteemed Contributor
Solution

Re: howto convert text files unix2dos using perl script ?

Chris,

I found this perl one liner from the net to get rid of the unix extra chars.,

perl -p -i -e 's/(?
No unix, no fun
Abdul Rahiman
Esteemed Contributor

Re: howto convert text files unix2dos using perl script ?

Hey ..

I tried my hands on some perl programming (for fun & learn) and come up with a script for your unix2dos translation.

Since I don't have the FTP module for perl, I could not test it with ftp though. But the translation seem to be working. Try this snippet,
------------------
$dfile="$file.dos";

open UFILE, "<$file" or die "cannot find $file file";
open DFILE, ">$dfile" or die "cannot create $dfile file";

# ---- Translate the control characters from unix format ----
foreach $line () {
$line =~ s/(?
No unix, no fun
'chris'
Super Advisor

Re: howto convert text files unix2dos using perl script ?

thanks !
but I have a little problem to run this script:
------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
use warnings;
use Net::FTP;
use Net::Netrc;

my $server = "x.x.x.x";
my $user = "anonymous";
my $password = "";
my $destination = "/tmp";
my $file = "/home/file.txt";
my $dfile="$file.dos";

open UFILE, "<$file" or die "cannot find $file file";
open DFILE, ">$dfile" or die "cannot create $dfile file";

# ---- Translate the control characters from unix format ----
foreach $line () {
$line =~ s/(?new ($server, Timeout => 9000, Debug => 3);
$ftp or die "$server: cannot connect: $@";
$ftp->login ($user,$password) or die "$_: Could not login: " . $ftp->message;
$ftp->cwd($destination);

$ftp->put($file) or die "$server: cannot put $file: " . $ftp->message;
$ftp->quit;

# ---- Delete the *.dos file ----
unlink($dfile) || die "Cannont unlink $dfile: $!";
------------------------------------------------------------

I get Error:
Global symbol "$line" requires explicit package name at test.cgi line 27.
Global symbol "$line" requires explicit package name at test.cgi line 28.
Global symbol "$line" requires explicit package name at test.cgi line 29.
Execution of test.cgi aborted due to compilation errors.

lines 27-29:
foreach $line () {
$line =~ s/(?
Jeroen Peereboom
Honored Contributor

Re: howto convert text files unix2dos using perl script ?

Chris,

add a my $line; somewhere at the top of the script!
Put the CLOSE statements after the } such that you first print all the lines and close the files after printing.

The script will run, but the number of lines is doubled?! Use 'wc' to check, and 'od -c' to check the output file.
Better remove the \n in the print statement...

JP
H.Merijn Brand (procura
Honored Contributor

Re: howto convert text files unix2dos using perl script ?

Easier and faster:

foreach my $line () {

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn