Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2015-07-07 23:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls