Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Convert end-of-line characters (revisited) UNIX
You can do a command-line conversion between Mac, PC, and Unix line endings using any one of many perl / sed / awk commands, or use the freeware command-line program flip. The nice thing about flip is that it's easy to remember and you don't have to make aliases, etc.

Usage is simple, just use one of the flags: -m, -d, or -u, for traditional Mac, DOS/Windows, or Unix line endings. A -t option will just tell you the type of a file. For example, flip -d *.txt will convert all files ending in .txt to DOS end-of-line characters.

[robg adds: This older hint explains (in the hint and the comments) how to use text editors and/or the command line to convert between Mac and Unix line breaks, but I thought it might be time for a new hint with another solution...]
    •    
  • Currently 2.83 / 5
  You rated: 4 / 5 (6 votes cast)
 
[122,781 views]  

Convert end-of-line characters (revisited) | 11 comments | Create New Account
Click here to return to the 'Convert end-of-line characters (revisited)' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
GUI tool: LineBreak
Authored by: blakers on Oct 21, '03 11:00:35AM

There's a very nice utility developed by Josh Aas called "LineBreak" that handles this nicely via GUI, including drag-n-drop:

"This is a simple utility for Mac OS X that converts line breaks in text documents"

It's GPL'd, and can be found at <http://www.macalester.edu/~jaas/linebreak.html>



[ Reply to This | # ]
flip.osx file
Authored by: 5chm31din6 on Oct 21, '03 11:08:31AM

So, what do I do with the downloaded flip.osx file?

---
your(better than a finger)eye

[ Reply to This | # ]

getting flip.osx file to work
Authored by: Bemopolis on Oct 21, '03 11:43:11AM

I'll assume a little Unix and CLI knowledge -- if not, this may not be a very useful command for you.

1. Download the file with a Command-click (to make sure binary will work properly).
2. Start a terminal and move the file to the directory where you keep any other Unix commands. You can also drop the '.osx' and
rename it if you wish.
3. Change permissions to allow users to execute flip.osx ('chmod 555 flip.osx' should do it).
4. Type 'rehash' and try it out with the '-t' option. If it doesn't work, double check your permissions and whatnot.

Bemopolis



[ Reply to This | # ]
getting flip.osx file to work
Authored by: 5chm31din6 on Oct 21, '03 01:25:06PM

Maximus gratis!

---
your(better than a finger)eye



[ Reply to This | # ]
Convert end-of-line characters (revisited)
Authored by: rjbailey on Oct 21, '03 02:12:23PM
To display mac text files at the command line in Terminal.app (or to pipe this to a new file), I created a script called "mac2unix" that contains one line:
cat $1 | tr '\r' '\n'

To convert unix files to Mac files, here is "unix2mac":
cat $1 | tr '\n' '\r'

To convert DOS files to unix files, here is "dos2unix":
cat $1 | tr -d '\r'

These employ the "tr" (translate character) utility.

[ Reply to This | # ]
Convert end-of-line characters (revisited)
Authored by: LC on Oct 21, '03 05:27:40PM

Need double-backslashes in this website (one gets
eaten by the remove slashes step, after previewing;)
I got bitten by that too; Larry.



[ Reply to This | # ]
Convert end-of-line characters (revisited)
Authored by: nick on Oct 21, '03 03:13:36PM

the freeware TextExtras provides an service-menu for line-ending-conversion. like all services its available in all cocoa-text-fields.



[ Reply to This | # ]
Convert end-of-line characters (AppleScript method)
Authored by: trekan on Oct 21, '03 05:05:28PM
You can also do this conversion in AppleScript. That is very practical if you're going to use files with foreign line endings in Applescripting.

In the following example we have just read the contents of a file and put it into fContents.

-- first we ask to put all the lines into a list (fParagraphs). AppleScript doesn't care if it is Unix, Mac or Windows.
set fParagraphs to paragraphs of fContents

-- now we tell AppleScript that lists will be concatenated using return characters when the list is converted to a string (Mac). This could also be "\n" for newline (UNIX), "\r\n" for character return+new line (Windows) or "\r" for character return (Mac). Don't be alarmed when you compile the script and see that \r and \n has changed into line breaks. The script still know that they are two different characters.
set AppleScript's text item delimiters to return

-- here we convert the list of paragraphs to strings using the text item delimiters
set fContentsNew to fParagraphs as string

-- here we reset the text item delimiters to "" which is default.
set AppleScript's text item delimiters to ""


[ Reply to This | # ]
dos2unix and unix2dos
Authored by: DahDee on Oct 22, '03 01:42:55AM
Another utility to accomplish this is dos2unix and unix2dos.
You can download the package from here:

ftp://us.osxgnu.org/pub/osxgnu/File_utils/unix2dos-1.2X.pkg.sit

USAGE: dos2unix [file ...]

DESCRIPTION
When called whithout parameters, the dos2unix utility reads from the standard input, transforming CR/LF pairs into an
LF character and writing it to the standard output. If there's at least one filename specified, the file operands are
processed in command line order, reading from the files specified and overwriting them with the converted version.
It's important to note that the dos2unix utility is the same program as the unix2dos(1) utility. It simply checks its name
to see what operation is expected. If you change the name of the binary, dos2unix will default to converting files from
UNIX to DOS.

[ Reply to This | # ]
I've always used zip and unzip...
Authored by: vajonez on Oct 22, '03 02:19:54AM
I've always used the following (in a file named fixascii):

#!/bin/sh
zip -qr foo.zip "$@" && unzip -aqo foo.zip && rm foo.zip

And then execute it as:

fixascii [files or directories to convert]

Which has the benefit over most of these other commands in that you can point it with impunity at an entire directory tree and it will process all the files in it and not corrupt any binaries that may happen to have a string of bits in them that look like a line-ending.

I've seen too many times where someone corrupted a ton of images and other binaries, when trying to fix line-endings on text files using dos2unix or tr in combination with find but failed to ensure that only text files were processed. Unzip figure out which files are ascii, converts them, and leaves the binaries alone.

BTW, it's the "-a" flag to unzip, that is causing the ascii files to have their lines endings converted.

[ Reply to This | # ]

I've always used zip and unzip...
Authored by: HotFusionMan on Jan 05, '09 02:49:45PM

Thanks! Couldn't resist optimizing this one a bit, though. :)

zip -qr0 foo.zip "$@" && unzip -aqo foo.zip && rm foo.zip

which does no compression and presumably will take less CPU time. Of course it requires more disk space....

I thought about factoring out the foo.zip into a variable but am too rusty in sh/bash syntax and have already spent too much time skimming through the bash man page. :)

I also thought about generating a (hopefully)-guaranteed unique temp filename rather than using foo.zip as is. I think you do that with $$ or similar.



[ Reply to This | # ]