Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Removing carriage returns from source code

by --OutpostMir-- (Initiate)
on Sep 06, 2001 at 07:17 UTC ( #110481=perlquestion: print w/replies, xml ) Need Help??

--OutpostMir-- has asked for the wisdom of the Perl Monks concerning the following question: (cgi programming)

I think I spent god-knows how long tonight working on setting up a Perl/CGI based image gallery for my site. Saddly, I finnaly get it coded properly (ha!) and I get this error when trying to use the script:

It looks like there was an error:
Your script produced this error: (Maybe you didn't strip carriage returns after a network transfer?)

My copy of Perl (ActivePerl) doesn't seem to have any tools to strip the errant carriage returns out of my script. What am I to do?

Originally posted as a Categorized Question.

  • Comment on Removing carriage returns from source code

Replies are listed 'Best First'.
Re: Removing carriage returns from source code
by blakem (Monsignor) on Sep 06, 2001 at 08:05 UTC
    How about:
    perl -pi.bak -e 's/\r\n/\n/g' myfile.pl perl -pi.bak2 -e 's/\r/\n/g' myfile.pl
    This will translate both Mac EOLs and MS EOLs into Unix like ones.

    -Blake

      Where is the answer here?I'm unable to find anywhere.
      Where is the answer here?I'm unable to find anywhere. Sateesh.. My mailid:sateesh_7@rediffmail.com
Re: Removing carriage returns from source code
by tachyon (Chancellor) on Sep 07, 2001 at 05:14 UTC

    In case you are unfamiliar with using Perl directly on the command line the -pi.bak part makes a backup file with a .bak extension before the -e (for execute) executes the 's/this/that/g' on every line of myfile.pl. For a more permanent solution just write a little script like this:

    #!/usr/bin/perl -w -i.bak use strict; while (<>) { s/this/that/g; print; }

    If you call this script say convert.pl you use it like this:

    C:\>type convert.pl #!/usr/bin/perl -w -i.bak use strict; while (<>) { s/this/that/g; print; } C:\>type this.txt this this this this C:\>perl convert.pl this.txt C:\>type this.txt that that that that C:\>type this.txt.bak this this this this C:\>

    Just change this and that to whatever you want. For Mac that will be \r and \n respectively.

Re: Removing carriage returns from source code
by TStanley (Canon) on Nov 03, 2001 at 22:19 UTC
    And if you are using the vi editor on *nix, you can do the following:
    :%s/^M//g

    To get the Control M character, press Control-V first, then Control-M. TStanley
Re: Removing carriage returns from source code
by Anonymous Monk on Nov 03, 2001 at 06:29 UTC
    It might be those annoying ^M's.
    <CODE>
    #!/usr/locl/bin/perl

    while(<>) {
    s/\cM//g;
    print;
    } <CODE>
    ~
    :w no_cM.pl
    % ./no_cM.pl < annoying_file.txt > ahhh.txt

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://110481]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-09-28 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?