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

I am writing a text file from a perl script in Windows. It adds a CR and LF. I try to use this text file on a Unix box, but it won't work because of the ^Ms at the end of lines. I can't dos2unix the file in unix environment due to permissions. Don't ask..... Is there a way to remove the ^Ms in my windows-based Perl script? I've read some earlier questions about this where people suggest s/\r//; This does not work. Any other ideas?

Replies are listed 'Best First'.
Re: dos2unix problem
by tune (Curate) on Oct 09, 2001 at 21:02 UTC
    I used to do the following from the shell:
    $ cat dosfile.txt | tr -d "\r" > unixfile.txt

    --
    tune

      Arggh! Not the dreaded "cat a single file into a pipe" command!

      This can always be replaced more efficiently by simply redirecting STDIN on the recieving command. Thus the example becomes

      $ tr -d "\r" < dosfile.txt > unixfile.txt
      which saves (approximately :) one process and two file handles.

      Of course, if you've got two files, you simply can't do this. And if the pipeline command is being generated from a script, the ease of building it this way may outway the efficiency of avoiding the extraneous "cat"

Re: dos2unix problem
by fokat (Deacon) on Oct 09, 2001 at 22:04 UTC
    All the incantations in the other sugestions so far, involve post-processing your script's output. I think your question was how to avoid the ^M at the end of each line in the first place.

    You can do that setting the file to "binary mode". I suggest you go look for binmode() in a "perldoc perlfunc" if you're using traditional filehandles.

    What happens is that "text" files in Win32 need to have its lines terminated in "\r\n", so a translation happens for/against you in the background whenever you write a text file. If you do not need this, simply declare your file to be binary as explained before. This will get rid of the extraneous "\r".

    Good luck.

Re: dos2unix problem
by Fletch (Bishop) on Oct 09, 2001 at 21:29 UTC
Re: dos2unix problem
by cLive ;-) (Prior) on Oct 09, 2001 at 21:48 UTC
    try:
    s/\r//g;
    The "g" modifier makes sure all instances get changed and not just the first.

    cLive ;-)

Re: dos2unix problem
by davorg (Chancellor) on Oct 10, 2001 at 12:03 UTC

    How do you transfer the file between Windows and Unix? If you use FTP in ASCII mode, then the FTP application will handle all the conversions for you.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: dos2unix problem
by c (Hermit) on Oct 09, 2001 at 21:07 UTC
    If the s/\r//; doesnt work, have you tried just doing something like s/\^M//g; to see if it would help you?

    humbly -c

      Why not use octal: \015

Re: dos2unix problem
by guidomortonski (Sexton) on Oct 11, 2001 at 15:43 UTC
    You could get yourself a text editor that writes unix linefeeds. EditPlus (http://www.editplus.com/) is quite nice, and does colour-coding of a variety of language's operators, functions etc (yes, including perl).

    Apart from that, if you copy the files in ascii mode rather than binary, that should also fix your problem.

    Guy