Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have got a dataset like this,2009-11-27,2009-11-27,2009-11-27

When I am running command to delete all of the same entries of column2

sort -t ',' -k2 stars.txt -u

It is deleting the entry of column1.

In order to delete the duplicate entries of column2, I am having to enter -k3 flag

sort -t ',' -k3 stars.txt -u

Can anyone explain to me why it is happening? Why I have to enter +1 to the column in the file to delete the column?

share|improve this question
Is it possible that the file contains CR characters? I would use sort -t, -k2,2 -u. -k2 is using the text starting at the second field as the sort key, while -k2,2 makes the second field alone the sort key. What version of sort it is. It suppose it's GNU sort since it accepts options after arguments, so sort --version should tell you. What does cat -vte on the file gives you? – Stéphane Chazelas Feb 4 '13 at 13:35
ECANTREPRODUCE – gelraen Feb 4 '13 at 13:35
@gelraen Can you explain to me what does this -k3,3? How is this -k3 different from -k3,3? – dwwdw Feb 4 '13 at 14:07
@dwwdw if you are on linux (might work elsewhere as well), see man sort, look for KEYDEF. – peterph Feb 4 '13 at 14:29
Please, read the man page. -k3 says that sorting key starts at column 3 (and spans to end of line), -k3,3 says that sorting key starts and ends at column 3. – gelraen Feb 4 '13 at 18:54

1 Answer 1

Just did this test

$ cat tfile,2009-11-26,2009-11-27,2009-11-28,2009-11-29,2009-11-27

Sort on k2 Works!

$ sort -t ',' -k2 tfile,2009-11-26,2009-11-27,2009-11-27,2009-11-28,2009-11-29

Sort on k2 with unique Works !

$ sort -t ',' -k2 tfile -u,2009-11-26,2009-11-27,2009-11-28,2009-11-29

sort of rogue key - Only prints first line

$ sort -t ',' -k3 tfile -u,2009-11-26

If you don't get these result, there must be something else on those lines.

share|improve this answer
but there are two column here, roght? site name and the date then why are we using 2 and 3 index for 1 and 2 columns – dwwdw Feb 4 '13 at 17:16
Yes, ;-) I only used k3 to show you it doesn't work as you originally stated. Use k1 or k2 depending on what column you wish to sort on. If it doesn't work maybe you have other characters on the lines which we cant see? – X Tian Feb 4 '13 at 17:26

Your Answer


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.