Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I'm trying to sort a file where typically (not always) the lines are

whatever:[A-Ba-b0-9_]: values

where fields are separated with :. Some lines do not follow this pattern, I'm not interested in them, they can be anywhere in the output.

I want to sort the lines only on the second column (and not on values), but when I do sort -t: -k 2 myfile, it sorts the file on all the line.

How to use the -k in order to have what I need?

share|improve this question

marked as duplicate by Gilles, Anthon, slm, vonbrand, Mat May 29 '13 at 11:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Which platform? sort has different options on Solaris and Linux. Also, is the field numeric or alphanumeric? – unxnut May 28 '13 at 15:05
I'm on Linux Mint 14. The filds are alphanumeric with also ', _, and other signs. – Jav May 28 '13 at 15:08
I tried your command on Ubuntu and it worked. May I suggest trying grep -v : myfile | sort -t: -k 2 to see if that gives you the desired result? – unxnut May 28 '13 at 15:12
All the line contain : – Jav May 28 '13 at 15:19
up vote 5 down vote accepted
sort -t: -k2

Sorts on the part of the line that goes from the second field to the end of the line. If you want to sort on the second field, you have to specify where the sort key ends:

sort -t: -k2,2

to sort on the second field only.

share|improve this answer

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