DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 17th May 2008
matt matt is offline
Port Guard
 
Join Date: May 2008
Posts: 13
Thanked 0 Times in 0 Posts
Default dos to unix linefeeds

I can't believe I don't know how to do this, but I don't know of an easy way to convert all of the files in a directory from dos linefeeds to unix ones. Surely there's a one-liner for this.
Reply With Quote
  #2   (View Single Post)  
Old 17th May 2008
18Googol2's Avatar
18Googol2 18Googol2 is offline
Real Name: whoami
Spam Deminer
 
Join Date: Apr 2008
Location: pwd
Posts: 283
Thanked 20 Times in 18 Posts
Default

You have a several choices. There is a port called dos2unix or you can use commands listed in this site:

http://www.vasudevaservice.com/docum...nix_text_files
__________________
The power of plain text? It can control an entire OS
Reply With Quote
  #3   (View Single Post)  
Old 17th May 2008
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

Hello,

There are two utilities for Unix systems that takes care of this issue. One is called unix2dos and the other is dos2unix. Some Unix systems include them in the base, some don't (but they are readily available on the internet if your system doesn't include them).
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
  #4   (View Single Post)  
Old 17th May 2008
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

Hello,

Looks like 18Googol2 beat me to it!
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
  #5   (View Single Post)  
Old 17th May 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

I usually use Perl but you can count on sed and awk being in every BSD, perl, unix2dos, and dos2unix on the other hand not so much.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #6   (View Single Post)  
Old 17th May 2008
lvlamb's Avatar
lvlamb lvlamb is offline
Real Name: Louis V. Lambrecht
Spam Deminer
 
Join Date: May 2008
Location: .be
Posts: 221
Thanked 25 Times in 24 Posts
Default

Alternatively, instead of just copy tje file, ftb, nfs, samba shoud take care of compatibility.
__________________
da more I know I know I know nuttin'
Reply With Quote
  #7   (View Single Post)  
Old 18th May 2008
protologic protologic is offline
New User
 
Join Date: May 2008
Posts: 5
Thanked 1 Time in 1 Post
Default

Here are one-liner functions I keep in my .shrc so I don't need to install dos2unix on each new machine:

dos2unix() { awk "{ sub(\"\r\$\", \"\"); print }" $1 > $2; }
unix2dos() { awk "sub(\"\$\", \"\r\")" $1 > $2; }
Reply With Quote
  #8   (View Single Post)  
Old 18th May 2008
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,118
Thanked 182 Times in 149 Posts
Default

For a single file:
Code:
$ tr -d '\r' <file_with_CRLF >file_without_CR
To do this for all files in a directory is left as an exercise for the student
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #9   (View Single Post)  
Old 19th May 2008
HitScan HitScan is offline
New User
 
Join Date: May 2008
Location: in.us
Posts: 8
Thanked 0 Times in 0 Posts
Default

Thanks J65nko, I was just about to ask why no was was showing any love for tr. It's very handy.
Reply With Quote
Old 19th May 2008
scottro's Avatar
scottro scottro is online now
Real Name: Scott Robbins
Spam Deminer
 
Join Date: Apr 2008
Location: NYC
Posts: 291
Thanked 31 Times in 25 Posts
Default

And, here's one for some types of Apple textfiles which are also full of ^Ms but in a different way.

tr '\r' '\n' < testfile > testfile.new

Thats actually not perfect, it will leave in a few little things here and there--only ran into this once when a vendor sent us something, but it at least makes the file readable.

Last edited by scottro; 19th May 2008 at 05:44 PM. Reason: typo
Reply With Quote
Old 10th September 2008
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

I just noticed that nvi doesn't handle dos line endings very well.
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
Reply

Tags
dos unix linefeeds

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unix Popularity ninjatux Off-Topic 147 21st January 2014 09:49 PM
College, Unix, and careers! bsdsys_x86 Off-Topic 8 18th October 2008 09:59 PM
How have you guys learned Unix administration? bigb89 Off-Topic 13 11th July 2008 03:40 PM
a tour through UNIX sources Oliver_H Off-Topic 2 25th June 2008 08:37 PM
Recommendation of the UNIX.COM Forums vermaden Off-Topic 53 24th June 2008 07:01 PM


All times are GMT. The time now is 03:13 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick