http://www.perlmonks.org?node_id=256366

boston_guy1 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Cygwin perl... ftp... Windows line endings...
by Rhose (Priest) on May 07, 2003 at 20:09 UTC
    You need to tranfer the files as ASCII type. To do this, you can use either the type() method, or, since you want ASCII, you can use the ascii() method in Net::FTP. (ASCII transfer will convert the end of line characters for you.)

    perldoc Net::FTP will give you more information.

Re: Cygwin perl... ftp... Windows line endings...
by halley (Prior) on May 07, 2003 at 20:06 UTC

    If the Net::FTP module handles the ASCII transfer mode, it should be able to request it of the server for those files which need it. Then one side strips out carriage returns in transit. That's what it's for. BINARY mode keeps both sides' bytes intact, which sounds like what's happening for you.

    Cygwin has a setting for whether or not the programs will work with \x0D\x0A or \x0A as the default value for \n. I don't know whether you've looked into that, or if it's something you can change in your circumstances.

    --
    [ e d @ h a l l e y . c c ]

      Thanks for your time. But if you could throw some light on how the stripping is done in transit. I am using ftp->ascii(). And i did not find anything more than this in the docs that would say, ok strip these text files for the line endings except the Hash. Which did not help much. Cheers K
Re: Cygwin perl... ftp... Windows line endings...
by cciulla (Friar) on May 07, 2003 at 21:01 UTC

    Can you use cygwin's dos2unix app?

    If you have a number of these files, you can iterate over them using:

    #!/usr/bin/perl $dirname = "."; opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { next if $file =~ /^\.\.?$/; system("dos2unix $file"); } closedir(DIR);

    This'll try to convert directories, too, but you did say you were in a hurry.


    I basically robbed The Perl Cookbook for more than 90% of this code. :)

Re: Cygwin perl... ftp... Windows line endings...
by tachyon (Chancellor) on May 08, 2003 at 04:07 UTC

    You need to set ASCII mode for the FTP. Read the docs. After the fact this is the standard perl one liner that will do all the files in the current dir (it will break binary files BTW unless you modify it. Ususally this is not an issue as there are no binary files in the same dir as your text files. Backups are written to *.bak for each file

    perl -pi.bak -e 's/\r//g' ./*

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Cygwin perl... ftp... Windows line endings...
by svsingh (Priest) on May 07, 2003 at 20:04 UTC
    I know you said this was a last resort, but you also mentioned it was urgent and I thought I'd help where I could. I don't know a Perl solution to change the line endings, but I use a freeware program called ToX (on Simtel) to convert line endings in Windows. You can select all of your text files in a windows directory and convert the line endings with a right-click.

    I hope you don't need to resort to this, but in case you do, I hope it helps.

      Thanks for your time. I know of the utils that would convert the line endings. Such as dos2unix and we have used it for other projects, But this one. Any other insights would be appreciated. K