PDA

View Full Version : [Resolved] Batch processing of text files - normalize line endings




wrldwzrd89
Mar 7, 2012, 07:02 AM
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.



balamw
Mar 7, 2012, 07:07 AM
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

wrldwzrd89
Mar 7, 2012, 11:41 AM
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.

balamw
Mar 7, 2012, 12:12 PM
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

ScoobyMcDoo
Mar 7, 2012, 02:23 PM
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 {} \;

balamw
Mar 7, 2012, 02:26 PM
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

wrldwzrd89
Mar 7, 2012, 03:20 PM
Got it!

Using the following terminal commands:

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.