Replace CR by CR LF
Question: I'm on Windows and I have an odd text file containing mostly CR+LF line
ending. A _few_ lines end with only CR. Which tool to use to transform these
odd lines into well formatted (e.g. CR+LF terminated) lines?

I could use either GnuWin32 tools or Python to solve this.

The main problem I have is that I cannot open the file as text file since
Python (as most other text processors, such as awk) don't recognize the mixed
line endings. So I believe the solution must incorporate binary processing of
the file.

The again, I cannot just replace CR by CR LF, since there are also CR LF line
endings existing that must not be touched.


Answer: To replace lines you can use regular expressions:

  * `\r+` to find CR
  * `\r\n` is the text you want as replacement text.

* * *

Regular Expressions in Python: [Regular
Expression](https://docs.python.org/2/howto/regex.html)

* * *
    
    
    import re
    txt='text where you want to replace the linebreak'
    out = re.sub("\r+", '\r\n', txt)
    print out
    

