Converting DOS filetype to UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting DOS filetype to UNIX
# 1  
Old 07-14-2012
Converting DOS filetype to UNIX

Hello folks

I am working on a project that requires me to write a script that operates on a bunch of text files. When I try
Code:
less file.txt

I see a bunch of ^M's everywhere. Some Googling tells me that this is because the files have a DOS fileformat and found the following fixes:
Code:
sed 's/^M$//' file.txt > output.txt

or
Code:
dos2unix file.txt

Alas, neither of these approaches seems to work. When I open the file in gedit, everything is formatted appropriately and if I use gedit to save the file as file1.txt, the new file is fine when I do
Code:
less file1.txt

too. Is there anything I can do to fix the file from command line?

---------- Post updated at 03:05 AM ---------- Previous update was at 03:00 AM ----------

Sorry folks, I just realized that the files are Mac OS Classic file format and not DOS. I wrote a little awk script that fixes things now. I'm very sorry for the confusion. I'd appreciate it if a moderator can remove the thread.

Thanks again
# 2  
Old 07-14-2012
Just for completeness if someone Googles the thread.
Had the O/S been any unix/Linux except early MACOS.

The quick way (delete the carriage-return characters using the correct method):
Code:
cat msdos_file.txt | tr -d '\r' > newfilename.txt


Note about dos2unix / dos2ux . These are not in-situ editors.
Code:
cat filename.txt | unix2dos > newfilename.txt


Last edited by methyl; 07-14-2012 at 07:13 PM.. Reason: layout
# 3  
Old 07-15-2012
More completeness: Try "recode" to convert between a wide variety of encodings. Can be installed using macports on the Mac.
Code:
$ recode -l

lists all encodings it knows about
Code:
$ recode old..new file

converts between "old" encoding and "new" one.
# 4  
Old 07-15-2012
Thanks to both of you. In addition, my way of doing it (from ARCHIVED: How do I convert between Unix and Mac OS or Mac OS X text files? - Knowledge Base) is
Code:
   awk '{ gsub("\r", "\n"); print $0;}' macfile.txt > unixfile.txt

to go from Mac OS to Unix/Linux.
# 5  
Old 07-15-2012
Quote:
Originally Posted by ksk
Thanks to both of you. In addition, my way of doing it (from ARCHIVED: How do I convert between Unix and Mac OS or Mac OS X text files? - Knowledge Base) is
Code:
   awk '{ gsub("\r", "\n"); print $0;}' macfile.txt > unixfile.txt

to go from Mac OS to Unix/Linux.
That is not a very good solution.

That approach will add an extra blank line during the conversion, because after converting the last \r to \n, the awk print statement will add another \n.

If the text file is very large, the conversion will also require a large amount of memory, because awk reads until it finds a \n record separator (which is absent). With some awks, if there's a line length limit, even if you had sufficient memory, the attempt would fail.

In my opinion, the tr utility is tailor-made for this task. It's widely available, simple to use, will not add an extra blank line, and will not consume large amounts of memory even when dealing with monstrous files.
Code:
tr '\r' '\n'

If you insist on using awk, the following is a much better approach (it won't add the extra blank line nor slurp the entire file into memory unless the entire file is one \r-terminated line):
Code:
awk 1 RS='\r' ORS='\n'

My critique aside, thank you for reporting back with your solution. It's always good (and helpful for those who will search the forum in the future) to know how problems were resolved.

Regards,
Alister

Last edited by alister; 07-15-2012 at 05:51 PM..
# 6  
Old 07-15-2012
Fair enough Alister :-) Thanks, I'll use tr from now on Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting DOS Batch file to Shell Script

Hi, This is my DOS Batch file. @echo off echo "Program Name :" %0 rem echo "Next param :" %1 echo "Next param :" "Username/Password" echo "User Id :" %2 echo "User Name :" %3 echo "Request ID ... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

2. UNIX for Dummies Questions & Answers

converting scripts from dos 2 unix format

Hi, I am new to shell scripting and exploring it , I have developed few sample shell script but I have developed them on windows xp notepad and then saving them on folder and then testing them on cywgin and running perfectly...but these scripts are in dos format and I want to convert them in unix... (1 Reply)
Discussion started by: rahul125
1 Replies

3. UNIX for Dummies Questions & Answers

UNIX/MS-DOs scipt

Is any one who know a good tutorial for Unix bash script and Ms-Dos scipt?? if yes, if is possible to upload it or give me the link??? What is the difference betwwen uvix and ms-dos script?? (1 Reply)
Discussion started by: Tom2
1 Replies

4. Shell Programming and Scripting

Dos-Unix Connectivity

Hi, Is there any method to connect unix box(Excecuting unix commands through Batch Files) through DOS prompt. (1 Reply)
Discussion started by: shekhar_ssm
1 Replies

5. Windows & DOS: Issues & Discussions

telnet from unix to dos

hi eveybody, i use sco unix as server and dos as client . how i can connect from unix server to dos client ( how to telnet to dos and run commands?)? thanks. (8 Replies)
Discussion started by: shrb78
8 Replies

6. SCO

telnet from unix to dos

hi eveybody, i use sco unix as server and dos as client . how i can connect from unix server to dos client ( how to telnet to dos and run commands?)? thanks. (1 Reply)
Discussion started by: shrb78
1 Replies

7. Cybersecurity

telnet from unix to dos

hi eveybody, i use sco unix as server and dos as client . how i can connect from unix server to dos client ( how to telnet to dos and run commands?)? thanks. (1 Reply)
Discussion started by: shrb78
1 Replies

8. Windows & DOS: Issues & Discussions

Converting UNIX scripts to DOS

Is there a tool available to convert UNIX (BASH Shell) scripts to DOS scripts? I understand that DOS scripting is far inferior to unix scripting, and therfore this conversion may not be possible. Alternativley, perhaps I could convert my Unix scripts to C... then compile it for a windows... (2 Replies)
Discussion started by: Crozz
2 Replies

9. Shell Programming and Scripting

Converting Shell script to Dos batch files

Hi friends! I am having some simple shell script files to build postgresql database and all. Now i want to convert those scripts to dos batch scripts(to run on windows XP/2000/NT) because there is no need of unix emulation for latest release of postgresql. Please somebody help me. (1 Reply)
Discussion started by: darwinkna
1 Replies

10. UNIX for Dummies Questions & Answers

DOS to Unix translator

I know nothing of unix and didn't know where to start. I've heard of a DOS to Unix translator, and since I know DOS pretty well, I thought that this program would be perfect. Any help you could give me would be appreciated. Bryan (1 Reply)
Discussion started by: bferguson
1 Replies
Login or Register to Ask a Question