Christoph
Christoph

Reputation: 115

Replace CR by CR LF

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.

Upvotes: 0

Views: 1410

Answers (1)

Andie2302
Andie2302

Reputation: 4897

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


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

Upvotes: 1

Related Questions