Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Removing carriage returns from source code

( #110481=categorized question: print w/ replies, xml ) Need Help??
Contributed by --OutpostMir-- on Sep 06, 2001 at 07:17 UTC
Q&A  > CGI programming


Description:

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?

Answer: Removing carriage returns from source code
contributed by blakem

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

Answer: Removing carriage returns from source code
contributed by tachyon

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.

Answer: Removing carriage returns from source code
contributed by TStanley

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
Answer: Removing carriage returns from source code
contributed by Anonymous Monk

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

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (11)
    As of 2014-07-22 21:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (128 votes), past polls