Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cygwin perl... Ftp... Windows line endings...

Status
Not open for further replies.

mqonnet

Programmer
Feb 13, 2003
152
US
Hi There,

I have a very peculiar problem.

I am using Cygwin perl Version 5.6.1 on win2k system. I am trying to FTP Windows ASCII/Text files to unix. I am getting windows line endings at the end of each line in the files that have been ftp'd using Net::Ftp module. These are CR/LF.

I dont want them to appear in the files that have been ftp'd onto unix. I want to remove them. I know there are some tools out there that would remove this for me. But i want to use them as the last resort. And i am approaching this forum to get any inputs on this.

If anyone could throw some light as to how i could convert a windows file with windows line ending to windows file with unix line endings before fpt'ing it accross to unix. Or to remove the windows line endings in general. I know of another way using "tr". But i dont want to use this either. As this is on a file by file basis. I wanted to know if there is any environment variable or any other way out with which i make minimal changes to my perl script and achieve the objective. Reason i am saying this is, my perl script has 100's of such files(ascii) being ftp'd and i have to add tr ahead of each one of them as it doesnt work for a bunch of files or using a wild character.

Any further info, please dont hesitate to post your responses.

Thanks in advance for your time.

K

Cheers
KK
 
I'm not sure what you're asking. You can convert a file in perl with a short script like this:
Code:
open IN, "in";
open OUT, ">out";
binmode IN;
binmode OUT;
while(<IN>)
{
    s/\r\n/\n/g;
    print OUT $_;
}
close OUT;
close IN;
You can change the mode of the FTP object itself to ascii if that's what you're using to transfer files. Something like this:
Code:
use Net::FTP;

my $ftp = Net::FTP->new('host');
$ftp->login('uid','pw');
$ftp->ascii();
$ftp->get('file');
$ftp->quit();
If this wasn't quite your question, could you point me in the right direction?

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
If you used Cygwin perl, you would know that this is the most common problem with it. Transferring ASCII/Text files from a windows platform to Unix platform is the concern here. If you use ftp module of perl, it does not appropriately get the unix line endings in place for these files and instead uses the windows line endings.

As for your options...

1) This is not an option. We cannot do it on file by file basis as already mentioned earlier.
2) Our script already uses ftp->ascii(). And the file is transferred fine with the Ftp module, but the whole issue is of the line endings.

Hope this clarifies a bit instead of confusing more... :)

Thanks for your time though....

Cheers
KK
 
Alright, I think I understand what you're saying now. I don't see any way to force cygwin into thinking that the local file system is windows and not unix. I don't know where it gets this information. Doing something stupid like resetting $^O to &quot;MSWin32&quot; had no effect, as expected.

If your problem with changing every local file individually was reaching every file, you could use File::Find to do the work for you.
Code:
use strict;
use warnings;
use File::Find;

my @dirs = ('.');

find(\&wanted,@dirs);

sub wanted
{
	#filter files, return unless file is named *.txt
	return unless(/\.txt$/);
	
	open FILE, &quot;$File::Find::name&quot; or return;
	binmode FILE;
	undef $/;	#slurp mode on
	my $data = <FILE>;
	close FILE;
	
	open FILE, &quot;>$File::Find::name&quot; or return;
	binmode FILE;
	$data =~ s/\r\n/\n/g;
	print FILE $data;
	close FILE;
}
Sorry if this still isn't what you're after, but I'm at a loss otherwise.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Icrf, thanks for your thoughts. Really appreciate it. But i was looking for more like some setting somewhere that would force cygwin to think that it needs to treat this windows file as if it were unix file or windows file with unix line endings.


Cheers
KK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top