Resolved Batch processing of text files - normalize line endings

Discussion in 'Mac Programming' started by wrldwzrd89, Mar 7, 2012.

  1. wrldwzrd89, Mar 7, 2012
    Last edited: Mar 7, 2012

    macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #1
    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.
     
  2. balamw, Mar 7, 2012
    Last edited: Mar 7, 2012

    Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    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
     
  3. thread starter macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #3
    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.
     
  4. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    Yes. Make sure by using "-v" it should ID itself as info-zip zip/unzip.

    B
     
  5. macrumors 65816

    Joined:
    Nov 26, 2007
    Location:
    Austin, TX
    #5
    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 {} \;
     
  6. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    FWIW that was the subject of the Wikipedia article I linked, which also included alternatives in perl and sed which do come preinstalled.

    B
     
  7. thread starter macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #7
    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.
     

Share This Page