On OSX, using tr to squeeze git diff results translates spaces to tabs

My issue is very simple to replicate, and I’ve only tested it on OSX with the default version of “tr” that comes bundled with 10.8.3.

Whenever I do a:

git diff --numstat <sha1> <sha2>

it results in the following:

1       1       test.php

Notice how the separating character between the ones are all spaces. Now, when I pipe that command directly into a tr to squeeze those spaces out as follows:

git diff --numstat <sha1> <sha2> | tr -s ' '

It results in all the spaces being converted to one single tab character (I tried to paste it here but it didn’t actually work).

The thing is, if I recall my bash correctly, this is not expected behavior at all. Also, when I tried to replicate this by putting this in a text file, cat-ing the text file and tr-ing it through the very same pipe, it does work as expected.

Does anyone know why this is and how would you work around it gracefully?

My end goal is to parse these results in python, which is significantly easier if there’s only one space separating the two numbers and the file name.

  • To hook or not to hook - git
  • One Solution collect form web for “On OSX, using tr to squeeze git diff results translates spaces to tabs”

    I’m pretty sure those are not spaces in your output line. I can duplicate your problem when I emit a line containing tab characters. Try this modification:

    git diff --numstat <sha1> <sha2> | tr -s ' \t' '  '

    The first group is a space and tab character, the second group is two spaces.

    Git Baby is a git and github fan, let's start git clone.