Python dos2unix one liner
@ Rocteur CC
macosx at rocteur.cc
Sat Feb 27 10:36:41 CET 2010
This morning I am working though Building Skills in Python and was
having problems with string.strip.
Then I found the input file I was using was in DOS format and I
thought it be best to convert it to UNIX and so I started to type perl
-i -pe 's/ and then I though, wait, I'm learning Python, I have to
think in Python, as I'm a Python newbie I fired up Google and typed:
+python convert dos to unix +one +liner
Found perl, sed, awk but no python on the first page
So I tried
+python dos2unix +one +liner -perl
But then I found http://wiki.python.org/moin/Powerful%20Python%20One-Liners
and tried this:
cat file.dos | python -c "import sys,re;
[sys.stdout.write(re.compile('\r\n').sub('\n', line)) for line in
And it works..
[10:31:11 incc-imac-intel ~/python] cat -vet file.dos
[10:32:10 incc-imac-intel ~/python] cat -vet file.unix
But it is long and just like sed does not do it in place.
Is there a better way in Python or is this kind of thing best done in
More information about the Python-list