Friday, 14 September 2007

Converting a DOS text file into Unix format

I was working on a config file in Ubuntu that had those nasty ^M
carriage returns at the end, and dos2unix doesn't seem to be around
anymore. So came across this info in Wikipedia:

The same tasks can be performed with sed, or in Perl if the platform
has a Perl interpreter:

sed -e 's/$/\r/' inputfile > outputfile # UNIX to
DOS (adding CRs)

sed -e 's/\r$//' inputfile > outputfile # DOS to
UNIX (removing CRs)

perl -p -e 's/(\r\n|\n|\r)/\r\n/g' inputfile > outputfile # Convert to DOS

perl -p -e 's/(\r\n|\n|\r)/\n/g' inputfile > outputfile # Convert to UNIX

perl -p -e 's/(\r\n|\n|\r)/\r/g' inputfile > outputfile # Convert to old Mac

It's probably the first time I've used sed!


David C said...

Why can't I use the above script to convert a Linux file into Windows format. This seems like such a trivial task but the script runs but doesn't actually do anything. It's getting really annoying!!!

krangsquared said...

Sorry to hear that. I just copied the above script from the Wikipedia entry mentioned. Have you checked if the util unix2dos exists on the system? That might be an easier way. What kind of file are you trying to convert?

Another way is to have a look at copying the file to a Windows system, then open it in a text editor like Notepad++ which can then save it with Windows style carriage returns.

David C said...

I'm on a windows system so I was trying to reverse the operation to replace the '\n' with a '\r\n'. I am using a plain HTML file, parsing it and then reformatting the data to be used in a C header file. I've got a work around where I just remove all the line stuff when I parse the file and it seems to be working. Another trick is to save the file with Wordpad and it sorts it out for you. Thanks for the fast response anyway.