Mac os x convert windows line endings

Random Stuff

Various ramblings of sysadmin, programmer, dancer, coffee snob, food lover and Winnipegger.

Monday, April 16, 2012

Convert Unix, Windows, Mac line endings using OS X command

Today I had to copy some MySQL data from Debian server into test environment on my MacBook. While importing data from tab delimited text files, I noticed warnings that data in the last column of several tables was being truncated. I looked at the tables and noticed MySQL doing some very strange formatting when printing them. It looked almost as if last column was padded with a bunch of white space. I opened import file in TextWrangler and it appeared fine, but when I looked in document options, I saw this:

The good ol’ EOL (end-of-line) character.

Different operating systems use different characters to mark the end of line:

  • Unix / Linux / OS X uses LF (line feed, ‘\n‘, 0x0A)
  • Macs prior to OS X use CR (carriage return, ‘\r‘, 0x0D)
  • Windows / DOS uses CR+LF (carriage return followed by line feed, ‘\r\n‘, 0x0D0A)

I’m guessing the person who sent me those files first transferred them to his Windows machine in ASCII mode, so newline characters got automatically converted during transfer.

Since some of the files were very big, instead of changing line endings in TextWrangler I decided to use command line (shocking, I know).

First I executed
to confirm existence of the dreaded ^M (carriage return) at the end of every line, and then ran
to generate new files without CR characters.

tr (translate character) is a nice little utility that does just that, substitutes one character with another or deletes it (like in my example). It’s available on pretty much any *nix distro so no need to install additional software.

Источник

Configure Visual Studio to use UNIX line endings

We would like to use Visual Studio 2005 to work on a local copy of an SVN repository. This local copy has been checked out by Mac OS X (and updates and commits will only be made under Mac OS X, so no problem there), and as a consequence the line endings are UNIX-style.

We fear that Visual Studio will introduce Windows-style line endings. Is it possible to force Visual Studio to use UNIX line endings?

9 Answers 9

Warning: This solution no longer works for Visual Studio 2017 and later. Instead, both of the answers by jcox and Munther Jaber are needed. I have combined them into one answer.

As OP states «File > Advanced Save Options», select Unix Line Endings.

This will only affect new files that are created. Fixing any that were previously created can be done file-by-file or you can search for tools that will fix on-bulk.

Читайте также:  Soundrecorder exe windows 10

Here are some options available for Visual Studio Community 2017

  1. «File > Advanced Save Options» has been removed by microsoft due to «uncommon use». Whatever that means. https://developercommunity.visualstudio.com/content/problem/8290/file-advanced-save-options-option-is-missed.html You can add it back by going to «Tools>Customize», then «Commands» tab, select the drop down next to «Menu Bar» select «File» then «Add Command»>File>Advanced Save Options..». You can then reorder it in the file menu by using «move down».

I don’t know if you will have to then set the advanced save options for each and every file, but it might prevent the issue I was having where my Visual Studio kept adding CL RF line endings into my files that were uniformly LF.

But I took it one step further and I added an extension called «Line Endings Unifier» by going to «Tools>Extensions and Updates>Online» and then searching for «line endings» in the search bar to the right. I will use this to automatically force all of my scripts to save with uniform line endings of my choice, but you can do more with it. https://marketplace.visualstudio.com/items?itemName=JakubBielawa.LineEndingsUnifier

strip’em is another solution that does something similar to Line Endings Unifier. http://www.grebulon.com/software/stripem.php

I am not sure how they differ or the advantages/disadvantages of either. I’m mainly using Line Endings Unifier just because it was in the Visual Studio Marketplace. I think I’ve used all of these methods in the past, but my memory is fuzzy.

Источник

How to change line-ending settings

Is there a file or menu that will let me change the settings on how to deal with line endings?

I read there are 3 options:

Checkout Windows-style, commit Unix-style

Git will convert LF to CRLF when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects, this is the recommended setting on Windows («core.autocrlf» is set to «true»)

Checkout as-is, commit Unix-style

Git will not perform any conversion when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects this is the recommended setting on Unix («core.autocrlf» is set to «input»).

Checkout as-is, commit as-is

Git will not perform any conversions when checking out or committing text files. Choosing this option is not recommended for cross-platform projects («core.autocrlf» is set to «false»)

7 Answers 7

The normal way to control this is with git config

For details, scroll down in this link to Pro Git to the section named «core.autocrlf»

If you want to know what file this is saved in, you can run the command:

and the git global config file should open in a text editor, and you can see where that file was loaded from.

Line ending format used in OS:

  • Windows: CR (Carriage Return \r ) and LF (LineFeed \n ) pair
  • OSX, Linux: LF (LineFeed \n )

We can configure git to auto-correct line ending formats for each OS in two ways.

  1. Git Global configuration
  2. Using .gitattributes file

Global Configuration

In Linux/OSX

This will fix any CRLF to LF when you commit.

In Windows

This will make sure that, when you checkout in windows, all LF will be converted to CRLF .

Читайте также:  Как запустить вредоносный файл windows 10

.gitattributes File

It is a good idea to keep a .gitattributes file as we don’t want to expect everyone in our team to set their own config. This file should be placed in the repository root and. If it exists, git will respect it.

This will treat all files as text files and convert to OS’s line ending on checkout and back to LF on commit automatically. If you want to specify the line ending explicitly, you can use:

The first one is for checkout and the second one is for commit.

This will treat all .jpg images as binary files, regardless of path. So no conversion needed.

Or you can add path qualifiers:

For a repository setting solution, that can be redistributed to all developers, check out the text attribute in the .gitattributes file. This way, developers dont have to manually set their own line endings on the repository, and because different repositories can have different line ending styles, global core.autocrlf is not the best, at least in my opinion.

For example unsetting this attribute on a given path [. — text] will force git not to touch line endings when checking in and checking out. In my opinion, this is the best behavior, as most modern text editors can handle both type of line endings. Also, if you as a developer still want to do line ending conversion when checking in, you can still set the path to match certain files or set the eol attribute (in .gitattributes) on your repository.

Also check out this related post, which describes .gitattributes file and text attribute in more detail: What’s the best CRLF (carriage return, line feed) handling strategy with Git?

For me what did the trick was running the command

inside the folder of the project, I wanted it specifically for one project.

That command changed the file in path /.git/config (fyi .git is a hidden folder) by adding the lines

at the end of the file. I suppose changing the file does the same trick as well.

Источник

Converting newline formatting from Mac to Windows

I need a conversion utility/script that will convert a .sql dump file generated on Mac to one readable on Windows. This is a continuation of a problem I had here. The issue seems to be with newline formatting in text files, but I can’t find a tool to make the conversion.

12 Answers 12

Windows uses carriage return + line feed for newline:

Unix only uses Line feed for newline:

In conclusion, simply replace every occurence of \n by \r\n .
Both unix2dos and dos2unix are not by default available on Mac OSX.
Fortunately, you can simply use Perl or sed to do the job:

This is an improved version of Anne’s answer — if you use perl, you can do the edit on the file ‘in-place’ rather than generating a new file:

You can install unix2dos with Homebrew

Then you can do this:

You can also convert dos files to unix:

Just do tr delete:

You probably want unix2dos:

You can either run unix2dos on your DOS/Windows machine using cygwin or on your Mac using MacPorts.

Читайте также:  Как расшифровывается kms windows

  1. Install dos2unix with homebrew
  2. Run find ./ -type f -exec dos2unix <> \; to recursively convert all line-endings within current folder

Here’s a really simple approach, worked well for me, courtesy Davy Schmeits’s Weblog:

Where foo is the file that has the Control+M characters at the end of the line, and foo2 the new file you are creating.

vim also can convert files from UNIX to DOS format. For example:

The following is a complete script based on the above answers along with sanity checking and works on Mac OS X and should work on other Linux / Unix systems as well (although this has not been tested).

On Yosemite OSX, use this command:

where the ^M sequence is achieved by pressing Ctrl + V then Enter .

Expanding on the answers of Anne and JosephH, using perl in a short perl script, since i’m too lazy to type the perl-one-liner very time.
Create a file, named for example «unix2dos.pl» and put it in a directory in your path. Edit the file to contain the 2 lines:

Assuming that «which perl» returns «/usr/bin/perl» on your system. Make the file executable (chmod u+x unix2dos.pl).

Example:
$ echo «hello» > xxx
$ od -c xxx (checking that the file ends with a nl)
0000000 h e l l o \n

Источник

Our Blog

Ongoing observations by End Point people

Convert Line Endings of Mac and Windows to Unix in Rails and Test with RSpec

Line endings or newline is a special character(s) to define the end of a line. The line endings special character(s) vary across the operating systems. Let’s take an example, we are developing a Rails feedback application which will be used by a wide range of users. The users might submit the feedback from different operating systems which has different kind of line end character(s). The content should have formatted for standard line endings before storing into backend.

Mostly two special characters used to define the line endings in most of the operating systems.

Line Feed (LF) — \n

Carriage Return (CR) — \r

The usage of these two special characters for Unix, Mac and Windows are

OS Characters Name
Unix \n LF
Mac \r CR
Windows \r\n CRLF

Note:- \r is the newline character up to Mac OS version 9, after that Mac uses Unix line endings.

It is a developer’s job to convert all kinds of line endings to Unix line ending format to maintain the standard. We can achieve this by a regex pattern replace. The regex pattern should convert \r(Mac) or \r\n(Windows) to \n(Unix).

We can see the conversion of line endings to Unix from Mac and Windows using irb (Interactive Ruby) shell.

1. Mac

2. Windows

RSpec Tests

After the implementation of line endings conversion, it should covered with test cases for the best practices of development. Here is the bunch of Rspec code to test both Mac and Windows line endings conversion.

The line endings conversion plays a crucial role in standardising the content. It is recommended to convert line endings to Unix style when providing web service features.

Источник

Оцените статью