LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-18-2005, 06:18 AM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Convert DOS text files to UNIX text files


I've got a lot of text files (source code) that I created in Windows, but then I moved them to Linux. Some of them retained their DOS text file format, but I would like them all to be in UNIX format. Is there a fast way to convert 50 or so files to UNIX? The way I've done it before is:

1) rename the file
2) create a new file with the old name
3) paste the text into it
4) delete the old file

I would be halfway willing to do that for the rest of them, except they all have at least one hard link, therefore they must be converted in place to retain the same inode. Thanks.
ta0kira
 
Old 05-18-2005, 06:30 AM   #2
madluther
Member
 
Registered: Aug 2004
Distribution: LFS
Posts: 350

Rep: Reputation: 31
Using tr is what i'd use to convert the files

tr -d '\r' < dosfile > unixfile

Retaining the same inode may require the use of an intermediate file.

HTH

Mad
 
Old 05-18-2005, 06:35 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I don't know about the hard link part but you may have success using tr to get rid of the \015 . After that is done, you could just move the new file back to the old file.
For example:
Code:
tr -d '\015' file.txt > file1.txt
mv file1.txt file.txt

Last edited by homey; 05-18-2005 at 06:36 AM.
 
Old 05-18-2005, 06:36 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,251

Rep: Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159
Maybe something like ...
Code:
sed -e 's/.$//' dosfile.txt > unixfile.txt
Merely deletes the last char on each line (hopefully CR), so be careful what you feed it.
 
Old 05-18-2005, 06:53 AM   #5
spaniel
Member
 
Registered: Mar 2005
Location: Netherlands
Distribution: Fedora Core 5 x86_64
Posts: 48

Rep: Reputation: 15
dont need the sed or tr

There is tiny little program for it (which does basically the same as sed and tr )

dos2unix and of course unix2dos

read the manpages for details.
Programs are usually located in /usr/bin (if you installed it there of course) and present in most distributions.
 
Old 05-18-2005, 07:16 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Thanks. I think Slack10 uses 'fromdos' which sounds like it does the same thing. I'll try that and let you know. Hopefully it leaves my already-UNIX files alone.
ta0kira
 
Old 05-20-2005, 02:09 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
I tried 'fromdos' which worked fine, except one or two already-UNIX were messed up. I had them backed up though. I had to use a temp file to keep the files in the same place.
ta0kira
 
Old 03-15-2011, 11:42 AM   #8
haveanother
LQ Newbie
 
Registered: Jan 2011
Posts: 13

Rep: Reputation: 0
I needed to convert an entire directory of files from dos to unix format, so I have used the following combination, inspired by madluther's post.

Code:
find | sed -e "s/\(.*\)/tr -d \'\\\r\' < \1 > __\1/g" | sed -e "s/__\.\//\.\/__/" | /bin/sh
find -iname "__*" | sed -e "s/.\/__\(.*\)$/\/bin\/mv .\/__\1 .\/\1/g" | /bin/sh
Possible issues with this:
-it gives warnings about directories
-it will not work with any files in sub-directories
-probably any files starting with '__' can be badly affected
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert text files to html files lothario Linux - Software 3 09-27-2005 08:48 PM
editing text files killertofu Linux - Newbie 2 10-15-2004 05:55 PM
convert CSV (TEXT) files to UTF-16 cccc Programming 1 07-01-2004 01:54 AM
how to convert windows formatted text files to *nix formatting? zero79 Linux - General 8 06-11-2004 03:00 AM
Unix to DOS text Wynd Programming 5 04-18-2003 12:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration