Python dos2unix one liner

Alf P. Steinbach alfps at
Sat Feb 27 18:40:01 CET 2010

* @ Rocteur CC:
> On 27 Feb 2010, at 12:44, Steven D'Aprano wrote:
>> On Sat, 27 Feb 2010 10:36:41 +0100, @ Rocteur CC wrote:
>>> cat file.dos | python -c "import sys,re;
>>> [sys.stdout.write(re.compile('\r\n').sub('\n', line)) for line in
>>> sys.stdin]" >file.unix
>> Holy cow!!!!!!! Calling a regex just for a straight literal-to-literal
>> string replacement! You've been infected by too much Perl coding!
> Thanks for the replies I'm looking at them now, however, for those who 
> misunderstood, the above cat file.dos pipe pythong does not come from 
> Perl but comes from:

Steven is right with the "Holy Cow" and multiple exclamation marks.

For those unfamiliar with that, just google "multiple exclamation marks", I 
think that should work... ;-)

Not only is a regular expression overkill & inefficient, but the snippet also 
needlessly constructs an array with size the number of lines.

Consider instead e.g.

import sys; sum(int(bool(sys.stdout.write(line.replace('\r\n','\n')))) for line 
in sys.stdin)

But better, consider that it's less work to save the code in a file than copying 
and pasting it in a command interpreter, and then it doesn't need to be 1 line.

>> Apply regular expression to lines from stdin
>> [another command] | python -c "import 
>> sys,re;[sys.stdout.write(re.compile('PATTERN').sub('SUBSTITUTION', 
>> line)) for line in sys.stdin]"
> Nothing to do with Perl, Perl only takes a handful of characters to do 
> this and certainly does not require the creation an intermediate file, I 
> simply found the above example on whilst searching 
> Google for a quick conversion solution.
> Thanks again for the replies I've learned a few things and I appreciate 
> your help.


- Alf

More information about the Python-list mailing list