Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Remove CR from file on Windows

by davidrw (Prior)
on Feb 10, 2007 at 04:53 UTC ( [id://599325]=note: print w/replies, xml ) Need Help??


in reply to Remove CR from file on Windows

this should work just fine in DOS (note you have to use double quotes for the -e param):
perl -pe "s/\r(?=\n)//" winfile.txt > unixfile.txt
see perlre for the postive look-ahead

or to do it in-place (see perlrun):
perl -i.bak -pe "s/\r(?=\n)//" winfile.txt > unixfile.txt
demo batch file:
perl -e "print \"foo\r\nbar\r\nstuff\";" > f.txt perl -pe "s/(\s)/'['.ord($1).']'/esg" f.txt REM output: foo[13][10]bar[13][10]stuff perl -i.bak -pe "s/\r(?=\n)//" f.txt perl -pe "s/(\s)/'['.ord($1).']'/esg" f.txt REM output: foo[10]bar[10]stuff

Replies are listed 'Best First'.
Re^2: Remove CR from file on Windows
by ikegami (Patriarch) on Feb 10, 2007 at 05:08 UTC
    None of those work in Windows. (I presume that's what you meant by DOS.)
    >echo Hello > winfile.txt >echo World >> winfile.txt >perl -pe "s/\r(?=\n)//" winfile.txt > unixfile.txt >debug unixfile.txt -rcx CX 0010 : -d100 l10 0AF7:0100 48 65 6C 6C 6F 20 0D 0A-57 6F 72 6C 64 20 0D 0A Hello ..W +orld .. -q

    Well, they might work with a simulated unix environment in Windows (like cygwin), but that's an entirely different world.

    The earlier posts in this thread not only provided the solution, they explained this problem and the workaround.

    If it's one-liners you wanted,

    • Works anywhere:
      perl -pe "BEGIN { binmode STDIN; binmode STDOUT } s/\x0D(?=\x0A)//" winfile.txt > unixfile.txt
    • Works on Windows:
      perl -pe "BEGIN { binmode STDIN }" winfile.txt > unixfile.txt
    • Works on unix:
      perl -ple 'BEGIN { $/="\r\n" }' winfile.txt > unixfile.txt
      I tested (in a DOS window on WindowsXP, striaght -- no cygwin) all of what i posted .. I don't understand -- why doesn't it work??
      C:\foo>type foo.bat @echo OFF echo "making test file" perl -e "print \"foo\r\nbar\r\nstuff\";" > f.txt echo "showing test file" perl -pe "s/(\s)/'['.ord($1).']'/esg" f.txt echo "altering file" perl -i.bak -pe "s/\r(?=\n)//" f.txt echo "showing result" perl -pe "s/(\s)/'['.ord($1).']'/esg" f.txt C:\foo>foo.bat "making test file" "showing test file" foo[13][10]bar[13][10]stuff "altering file" "showing result" foo[10]bar[10]stuff C:\foo>

        You think your solution works because your script to create the test file and your script to dump the test file suffers from the same bug as your solution. Your demonstration only shows that you can convert CRCRLF to CRLF.

        The first f.txt contains foo\r\r\nbar\r\r\nstuff.

        >perl -e "print \"foo\r\nbar\r\nstuff\";" > f.txt >debug f.txt -rcx CX 0011 : -d100 l11 0AF7:0100 66 6F 6F 0D 0D 0A 62 61-72 0D 0D 0A 73 74 75 66 foo...bar +...stuf 0AF7:0110 66 f -q

        You alter it to foo\r\nbar\r\nstuff.

        >perl -i.bak -pe "s/\r(?=\n)//" f.txt >debug f.txt -rcx CX 000F : -d100 lf 0AF7:0100 66 6F 6F 0D 0A 62 61 72-0D 0A 73 74 75 66 66 foo..bar. +.stuff -q

        why doesn't it work??

        Unless you use binmode on your input file, CRLF gets converted to LF on read.
        Unless you use binmode on your output file, LF gets converted to CRLF on write.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found