Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Today I thought I was doing a simple operation:

git blame file | sort -k 3

To get all the lines of file sorted by the date on which they were last changed. Unfortunately it doesn't seem to be sorting it correctly. It does work if I do the following:

git blame file | cut -c 20- | sort

Which just hacks the first part of the line off and then does the sort. The success of this command indicates to me that sort can in fact do the operation I was trying. Why didn't it work?

Here's an example line from the output of git blame file:

35d8e9eb (username 2007-01-17 03:58:04 +0000 155) Some text on line 155

Edit: Using any reasonable number besides 3 in the original command line seems to work fine - I can sort by the hash (field 1), the username (field 2), the time of day (field 4), or the line number (field 6), but the date (field 3) doesn't work - I can't identify at all what order it actually ends up in...

Edit 2: I've made a sanitized input file that shows the same problem. Here's cleaned.txt:

cb7bb041 (eeeeeee  2010-12-14 19:41:18 +0000  42)
35d8e9eb (cccccccc 2007-01-17 03:58:04 +0000 135)
d7377fa9 (hhhhhhhh 2010-01-30 04:26:28 +0000 178)

And the output from sort -k 3 cleaned.txt:

$ sort -k 3 cleaned.txt 
cb7bb041 (eeeeeee  2010-12-14 19:41:18 +0000  42)
35d8e9eb (cccccccc 2007-01-17 03:58:04 +0000 135)
d7377fa9 (hhhhhhhh 2010-01-30 04:26:28 +0000 178)

As you can see the sorting on the date field didn't appear to take place at all. Here's the output from cut -c 20- cleaned.txt | sort:

$ cut -c 20- cleaned.txt | sort
2007-01-17 03:58:04 +0000 135)
2010-01-30 04:26:28 +0000 178)
2010-12-14 19:41:18 +0000  42)

Sorting on dates in that case works fine! Any suggestions?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I just figured it out. The shorter username eeeeeee means there's an extra space before the date field. Since the field separator for sort is a non-blank to blank transition, the date field for the line with the shorter username has that space as part of the key field, and gets sorted first. Simple fix:

git blame file | sort -b -k 3
share|improve this answer
1  
You also have to watch out for author names that have different numbers of spaces in them. Your examples all have single-word author names (eeeeeee, cccccccc, and hhhhhhhh), but real names will often vary (e.g. git.git has author names with as few as one and up to five space separated “words”). Using -e might help; the email addresses it shows are much less likely to have any spaces. –  Chris Johnsen Mar 24 '11 at 3:05
    
@Chris - good point. This was just a one-off that confused me, so I'm not worried about it in general, but that's good to remember if I were to script this up. –  Carl Norum Mar 24 '11 at 3:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.