Python dos2unix one liner

Grant Edwards invalid at invalid.invalid
Sat Feb 27 19:02:34 CET 2010


On 2010-02-27, @ Rocteur CC <macosx at rocteur.cc> wrote:
>
> 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:
>
> http://wiki.python.org/moin/Powerful%20Python%20One-Liners
>
>> 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,

In _theory_ you can do a simple string-replace in situ as long
as the replacement string is shorter than the original string.
But I have a hard time believing that Perl actually does it
that. Since I don't speak line-noise, will you please post the
Perl script that you claim does the conversion without creating
an intermediate file?

The only way I can think of to do a general in-situ file
modification is to buffer the entire file's worth of output in
memory and then overwrite the file after all of the processing
has finished.  Python can do that too, but it's not generally a
very good approach.

-- 
Grant




More information about the Python-list mailing list