Convert UNIX formated text files to DOS formated?

W

walterbyrd

I have about 150 unix formated text files that I would like to convert
to dos formated.

I am guessing that I loop though each file in the directory, read each
line and conver the last character, then save to a file with the same
name in another directory.

I am not really sure what I convert the last charactor to.
 
D

Diez B. Roggisch

walterbyrd said:
I have about 150 unix formated text files that I would like to convert
to dos formated.

I am guessing that I loop though each file in the directory, read each
line and conver the last character, then save to a file with the same
name in another directory.

I am not really sure what I convert the last charactor to.

Use recode.

Diez
 
M

MRAB

walterbyrd said:
I have about 150 unix formated text files that I would like to convert
to dos formated.

I am guessing that I loop though each file in the directory, read each
line and conver the last character, then save to a file with the same
name in another directory.

I am not really sure what I convert the last charactor to.

The quickest and OS-agnostic way would be:

text = open(path, "U").read()
text = text.replace("\n", "\r\n")
open(path, "wb").write(text)

That way it doesn't matter if the text file is already dos formatted.
 
W

walterbyrd

The quickest and OS-agnostic way would be:

     text = open(path, "U").read()
     text = text.replace("\n", "\r\n")
     open(path, "wb").write(text)

That way it doesn't matter if the text file is already dos formatted.


Thanks, I am not familiar with the "U" here is how I did it:

------------
import os

for file in os.listdir('.'):
infile = open(file,'r')
outfile = open( 'new_' + file, 'w')
for line in infile:
line = line.rstrip() + '\r\n'
outfile.write(line)
infile.close()
outfile.close()
 
M

MRAB

walterbyrd said:
Thanks, I am not familiar with the "U" here is how I did it:

------------
import os

for file in os.listdir('.'):
infile = open(file,'r')
outfile = open( 'new_' + file, 'w')
for line in infile:
line = line.rstrip() + '\r\n'
outfile.write(line)
infile.close()
outfile.close()

That will also strip any whitespace off the end of the lines.

FYI, if you run it on Windows then the resulting lines will end with
"\r\n\n" because the output file is opened in text mode, which will
write any "\n" as "\r\n".
 
T

Terry Reedy

walterbyrd said:
I have about 150 unix formated text files that I would like to convert
to dos formated.

Are you sure you need to do that? Most Windows programs (including
Python) are happy reading text files with just \n for line endings.
 
N

norseman

walterbyrd said:
I have about 150 unix formated text files that I would like to convert
to dos formated.

I am guessing that I loop though each file in the directory, read each
line and conver the last character, then save to a file with the same
name in another directory.

I am not really sure what I convert the last charactor to.
===================
Subject line says UNIX to DOS

I hope that means you are using a UNIX machine.

addcr.scr
========
#!/bin/bash
#
cat $1 | sed s/$/$'\x0d'/ >$1.cr
#
# end of file
========
I have utils on MSDOS that add the cr to get the crlf sequence
I wrote it for use in the rare occasion. (my definition of rare. :)




rmcr.scr
========
#!/bin/sh
# rmcr.scr
# removes <cr> from text files
# Date: Feb. 2004
# by: SLT
# : file dressed up Dec. 2004
#
# a literal <cr> follows first slash following the s.
#
i=$1
i1=${i%%.*}
echo $i1
cat $1 | sed s/^M// >$i1._cr
#
# end of file
===========
The ^M will needs to be entered at 'type in' time.
In vi it is done by pressing CTRL-V CTRL-M in sequence
DO NOT CUT n PASTE that line!!!
I have never been able to use the $'\x0d' in this file.
The MSDOS programs don not run on Linux, per se. 'Ya Think' :)


crlf \r \n x'0D' x'0A' CTRL-M CTRL-J
carriage return, line feed keep the pair in the order shown.



to use in UNIX:

(be in a bash shell)
bash
for f in *.txt; do addcr.scr $f; done # going to MSDOS

for f in *.txt; do rmcr.scr $f; done # returning from MSDOS

if you run these on a binary - the binary is destroyed!!!


If you are in MSDOS, I have no help.
Well - see if you can find and download FILT.EXE (ver 1.0)
from the 1980 something era
FILT <Enter> will present the helpfiles
It's what I use. I set up batch (.BAT) files to expedite my desires.


Best of Luck


Steve
 
S

Simon Forman

I have about 150 unix formated text files that I would like to convert
to dos formated.

I am guessing that I loop though each file in the directory, read each
line and conver the last character, then save to a file with the same
name in another directory.

I am not really sure what I convert the last charactor to.

Python source includes tool for this: lfcr.py

http://svn.python.org/view/python/trunk/Tools/scripts/lfcr.py?view=markup

Depending on how python was installed on your system you might already
have this script on your system. (FYI there's also a crlf.py script
in the same directory too.)

HTH
~Simon
 
W

walterbyrd

Are you sure you need to do that?  Most Windows programs (including
Python) are happy reading text files with just \n for line endings.

These files will be looked at by some non-technical people. I am sure
these people will just click on the icons, and the file will come up
in notepad.

But, you are correct, I know that wordpad, and notepad++, and many
other apps will read the UNIX formated files just fine. IMO: notepad
is total junk, but it's still the default text editor on windows.
 
W

walterbyrd

Subject line says UNIX to DOS

I hope that means you are using a UNIX machine.

I should have mentioned, I am working in an environment that is very
restrictive about what I can put on my XP desktop. I can not put
python, or even notepad++, on my desktop. But, I do have cygwin.

As you probably know, cygwin formats to UNIX.
 
W

walterbyrd

Thanks for shell script code. That code may be just as efficient, or
even more efficient, than python. But, to me, python is far more
readable.

------------
i=$1
i1=${i%%.*}
echo $i1
cat $1 | sed s/^M// >$i1._cr
-----------=


------------
import os

for file in os.listdir('.'):
infile = open(file,'r')
outfile = open( 'new_' + file, 'w')
for line in infile:
line = line.rstrip() + '\r\n'
outfile.write(line)
infile.close()
outfile.close()
------------
 
J

Jeroen Ruigrok van der Werven

-On [20090513 05:53] said:
As you probably know, cygwin formats to UNIX.

That entirely depends on how you install it.

Anyway, I am sure the Cygwin repository has the tools dos2unix/unix2dos
available for installation.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
In short may I, directly and indirectly, offer benefit and happiness to all
beings, may I secretly take upon myself the harm and suffering of all
beings...
 
D

David Robinow

I should have mentioned, I am working in an environment that is very
restrictive about what I can put on my XP desktop. I can not put
python, or even notepad++, on my desktop. But, I do have cygwin.

As you probably know, cygwin formats to UNIX.

cygwin has u2d and d2u
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top