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

Converting Mac linefeeds to Unix linfeeds UNIX
I go back and forth between Darwin (and Linux at my workplace) and OS X all the time. The problem is that the convention for linefeeds is different between Unix and Mac. I know new version of MacArmyKnife can fix it, but here is the free solution.

Open the file with xemacs. As the bundled emacs does not work for this purpose, I've installed a copy through fink.

Now type:
Meta-x replace-string
^Q^M
^Q^J
This changes ^M (Mac linefeeds) to ^J (Unix linefeeds) for the entire buffer.

Save it:
^X^S
and you're done.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[5,266 views]  

Converting Mac linefeeds to Unix linfeeds | 19 comments | Create New Account
Click here to return to the 'Converting Mac linefeeds to Unix linfeeds' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
BBEdit and Tex-Edit
Authored by: kerim on Mar 06, '02 10:40:03PM

BBEdit and Tex-Edit (not TextEdit) both do this as well. BTW, although not free, BBEdit does almost everything emacs does and is OSX native...



[ Reply to This | # ]
An alias that uses perl
Authored by: TheGS on Mar 06, '02 10:47:32PM
Rather than loading a file into a text editor (like emacs or BBEdit), I have an alias set up that does the conversion, using perl instead:

alias mac2unix "perl -pi -e 'tr/\015/\012/' \!*"

And then I can use it directly from the command line:

% mac2unix MacTextFile.txt ...



[ Reply to This | # ]
An alias that uses perl
Authored by: TheGS on Mar 07, '02 12:00:15AM
Somebody else suggested that a drag-and-drop tool would be useful. Here's a quick AppleScript droplet that accepts files dropped onto it (ignoring directories) and then passes the absolute path of each file to a one-line perl script.
 on open fileList
   repeat with x in fileList
     if folder of (info for x) is not true then
       set y to POSIX path of x
       do shell script ("perl -pi -e \"tr/\\015/\\12/\" " & y)
     end if
   end repeat
 end open


[ Reply to This | # ]
An alias that uses perl
Authored by: oink on Mar 08, '02 01:55:31PM

can I use Logan's "alias mac2unix="perl -p0015l0012e '' -i $*"" with this script

on open fileList
repeat with x in fileList
if folder of (info for x) is not true then
set y to POSIX path of x
do shell script ("perl -p0015l0012e '' -i" & y)
end if
end repeat
end open

doesn't work... no error, just wouldn't convert.

Thanks



[ Reply to This | # ]
An alias that uses perl
Authored by: TheGS on Mar 08, '02 09:05:18PM

You might be missing a space between the '-i' and the path to the file.



[ Reply to This | # ]
An alias that uses perl
Authored by: logan on Mar 07, '02 12:15:36AM
Yeesh. A one-liner? How about a zero-character script that does the same thing? ;)
alias mac2unix="perl -p0015l0012e '' -i $*"


[ Reply to This | # ]
An alias that uses perl
Authored by: TheGS on Mar 07, '02 12:25:27AM

I like yours better — there's less time wasted compiling that 'tr' operation. TMTOWTDI, in any case.



[ Reply to This | # ]
An alias that uses perl
Authored by: oink on Mar 08, '02 01:52:48PM

re: alias mac2unix="perl -p0015l0012e '' -i $*"

Hmm, wow.

Now all I need is to fit this into an applescript, the following doesn't work.

on open fileList
repeat with x in fileList
if folder of (info for x) is not true then
set y to POSIX path of x
do shell script ("perl -p0015l0012e '' -i" & y)
end if
end repeat
end open




[ Reply to This | # ]
An alias that uses perl
Authored by: ret on Mar 14, '02 09:35:34PM

That's an l (lower case "L"), not a "|" pipe. See what a difference that makes to your script.

cheers
RET



[ Reply to This | # ]
emacs? (hawk, spit)
Authored by: ret on Mar 06, '02 11:35:57PM

You can also do this in vim, which if you haven't installed instead of the stock vi, you really should.

open the file (you'll see the line-ending type in the status line if it's not the default one)
type:
:set ff=unix
- or -
:set ff=mac
-or -
:set ff=dos

to set what you want the format to be, and
:wq

to write and quit.

Even so, a drag-n-droppable tool would be better.

cheers
RET



[ Reply to This | # ]
GUI and free: Project Builder
Authored by: JoeHope on Mar 06, '02 11:47:17PM

A GUI(Aqua) and free method is to use Project Builder, which comes with DevTools.

Under "format" you have the choice of Mac, Unix or Windows line endings.
It also gives you a large choice of file encodings.



[ Reply to This | # ]
vi instructions
Authored by: ragnar on Mar 07, '02 11:25:42AM

For the vi folks (I'm sure there are a vew) this is how you do it:

:%s/^M/^J/g

Easy, eh?



[ Reply to This | # ]
vi instructions
Authored by: ret on Mar 08, '02 12:47:57AM

And if you've got a DOS-sourced file, it is:

:%s/^M//

By the way, to get the ^M, don't type a literal caret-m. Type ctrl-v then ctrl-m. The ctrl-v tells vi that the next char you type is a special character (this is how you embed an escape character or backspace also).

cheers
RET



[ Reply to This | # ]
This is NOT the correct way to do this in emacs!!!
Authored by: Cadre on Mar 07, '02 04:02:50PM

Emacs has builtin functionality to convert between file types.

Type control-x return f

Then type undecided-unix, undecided-mac, or undecided-dos for what format you want the file changed too.



[ Reply to This | # ]
This is NOT the correct way to do this in emacs!!!
Authored by: richard k. belew on Mar 13, '02 07:01:16AM

thanks cadre, this was the hack i was
looking for. i knew it was in emacs
somewhere (everything is in a kitchen sink:)
but couldn't find just where.

rik



[ Reply to This | # ]
Why?
Authored by: paploo on Mar 07, '02 06:27:32PM

A quicker way for the command line inclined is to use the command `tr':

Say you have a text file, myText.txt, and you want to translate the carriage returns to new line characters. Then boot up the terminal, cd to the proper directory, and then type:

cat myText.txt | tr "r" "n" > out.txt

Where out.txt is the name of the converted text file.

Granted, if you were doing this a lot, you should make a shell script and stick it in your ~/bin directory. I would probably make a Perl script, just because I'm more comfortable with that.

For the curious, sed could also get the job done.

-Jeff



[ Reply to This | # ]
Why?
Authored by: rbrakes on Mar 08, '02 09:34:25AM

Close, but not quite. I think the command you list above will replace all the 'r' characters with 'n' characters in the text file which is not the desired result.

Here is the command I regularly use:

tr 'r' 'n' < infile.txt > outfile.txt

The trick is to use the backslash to represent the CR and NL characters respectively.



[ Reply to This | # ]
Why?
Authored by: pmccann on Mar 08, '02 11:31:14AM

And the band played on...

This must be my cue to come in an ask if the attack of the invisible backslashes can be cured. Please! It's happening again and again. Apologies to those who have seen me whine about this already.

I dread to think what might happen if someone posts a nice command which requires a backslashed escape in a file path and someone cuts and pastes. (Sure, the user should know exactly what's being done before doing it, but bets on not everyone obeying that maxim?)

Cheers,
Paul (complete with melodrama this time...)



[ Reply to This | # ]
Why?
Authored by: TheGS on Mar 08, '02 09:11:50PM

I think you can present a backslash\ by entering it as an HTML entity (&#092;), but it might not survive the preview.



[ Reply to This | # ]