Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 7, 2012, 07:02 AM   #1
wrldwzrd89
macrumors G4
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
Batch processing of text files - normalize line endings

Right, so I have 200+ plain text files that all need the same fix: Change their line endings to DOS/Windows format. Is there a simple way to do this with the Terminal and/or AppleScript? They're not all in the same folder, but writing a shell script to traverse the folder structure shouldn't be too hard. What I am looking for is the commands to use to apply the fix to each text file. If this is better done with AppleScripting of TextWrangler, so be it. I'm fine with that.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, 10.8.4

Last edited by wrldwzrd89; Mar 7, 2012 at 03:20 PM. Reason: Resolved
wrldwzrd89 is offline   0 Reply With Quote
Old Mar 7, 2012, 07:07 AM   #2
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
FWIW My cheap and sleazy way around this is to zip the files and use info-zip unzip's -a option to treat all files as ASCII, but that won't work to get dos endings on a unix system.

EDIT: This Wikipedia article may come in handy. http://en.wikipedia.org/wiki/Unix2dos. If the files are a mixed bag already, I'd use the zip approach mentioned above to normalize the files to unix endings before translating. Otherwise you run the risk of getting some funky line endings like \r\r\n on some occasions.

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme

Last edited by balamw; Mar 7, 2012 at 07:57 AM.
balamw is offline   0 Reply With Quote
Old Mar 7, 2012, 11:41 AM   #3
wrldwzrd89
Thread Starter
macrumors G4
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
Quote:
Originally Posted by balamw View Post
FWIW My cheap and sleazy way around this is to zip the files and use info-zip unzip's -a option to treat all files as ASCII, but that won't work to get dos endings on a unix system.

EDIT: This Wikipedia article may come in handy. http://en.wikipedia.org/wiki/Unix2dos. If the files are a mixed bag already, I'd use the zip approach mentioned above to normalize the files to unix endings before translating. Otherwise you run the risk of getting some funky line endings like \r\r\n on some occasions.

B
Thanks, I'll give this a try. What's info-zip? If that's the CLI zip utility, I think I'm good to go.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, 10.8.4
wrldwzrd89 is offline   0 Reply With Quote
Old Mar 7, 2012, 12:12 PM   #4
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Quote:
Originally Posted by wrldwzrd89 View Post
Thanks, I'll give this a try. What's info-zip? If that's the CLI zip utility, I think I'm good to go.
Yes. Make sure by using "-v" it should ID itself as info-zip zip/unzip.

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw is offline   0 Reply With Quote
Old Mar 7, 2012, 02:23 PM   #5
ScoobyMcDoo
macrumors 65816
 
Join Date: Nov 2007
Location: Austin, TX
The other way is to use the utility, unix2dos. This does not appear to come stock on OS X, so you will have to get it via fink or macports, or perhaps just d/l the code.

Anyway, once that is installed you could use the command:

find . -name *.txt -exec unix2dos {} \;
ScoobyMcDoo is offline   0 Reply With Quote
Old Mar 7, 2012, 02:26 PM   #6
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Quote:
Originally Posted by ScoobyMcDoo View Post
The other way is to use the utility, unix2dos.
FWIW that was the subject of the Wikipedia article I linked, which also included alternatives in perl and sed which do come preinstalled.

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw is offline   0 Reply With Quote
Old Mar 7, 2012, 03:20 PM   #7
wrldwzrd89
Thread Starter
macrumors G4
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
Got it!

Using the following terminal commands:
Code:
cd (drag directory to be fixed here)
find . -name *.txt -exec perl -i -p -e 's/\r//' file \;
find . -name *.txt -exec perl -i -p -e 's/\n/\r\n/' {} \;
The second command fixes everything to UNIX style.
The third changes the files to Windows style, as I want.

Fortunately, I had no Mac-style text files.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, 10.8.4
wrldwzrd89 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Batch audio normalize for iTunes library CocoaNut Mac Applications and Mac App Store 1 Mar 27, 2014 11:46 AM
[MP4Tools] - Batch Processing? DaPhatty Apple TV and Home Theater 1 Jun 16, 2013 01:14 PM
Line endings youds Mac Applications and Mac App Store 1 May 9, 2013 02:51 PM
Massive Handbrake batch processing - need guidance m021478 Mac Applications and Mac App Store 10 Feb 26, 2013 07:23 PM
Batch processing Dfine 2.0 with Aperture Prodo123 Digital Photography 0 Sep 9, 2012 03:14 PM

Forum Jump

All times are GMT -5. The time now is 03:21 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC