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

a simple yet frustrating question: i have a linux shell script (actually the linux q3 demo :-) ) which i obtained from a windows using friend with more bandwidth than me :-(

the problem is that somewhere along the line, all of the end-of-line chars in the script end in the windows end-of-line char, which looks like an underscored 'M' in my editor. what is the Quick'n'Dirty TM solution to this?

cat 'file' | perl -ne 's/\m$/\n/g; print' > 'newfile' doesn't work (the \m was just a guess...)

cheers,
d_i_r_t_y

Replies are listed 'Best First'.
(tye)Re: converting windoze end-of-line chars to unix
by tye (Sage) on Dec 29, 2000 at 04:35 UTC

    Here is a one liner that "fixes" text file newlines to match your current platform (so will make a DOSish file UNIXy under UNIX but make a UNIXy file DOSish under DOS).

    perl -pe "s/\r*$//" infile >outfile
    Note that it doesn't handle the very silly CTRL-Z-is-EOF of DOS, which I haven't had problems with recently.

    \m was a good guess but \Cm or \r is what you wanted. No need to use "cat". "-p" is like "-n" except you don't need to have the "print".

            - tye (but my friends call me "Tye")
      If I may improve (slightly)
      perl -i.orig -pe "s/\r*$//" file
      The -i.orig makes a temp file (file.orig) incase the conversion doesn't work right.
Re: converting windoze end-of-line chars to unix
by fundflow (Chaplain) on Dec 29, 2000 at 04:29 UTC
    This was here so many times...

    You should do s/\r\n/\n/g

      i know it's probably a FAQ, but try searching for it, dude... 'convert windows line end characters' produces not a lot...

      thanks for the reply though

        You are welcome.

        I know what you mean. Today it happened to me as well.

        That's why we have PM! :)