4

I have got a dataset like this

tack2@domain.com,2009-11-27

overflow@domain2.com,2009-11-27

overflow@domain2.com,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?

6
  • 2
    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? Feb 4, 2013 at 13:35
  • ECANTREPRODUCE privatepaste.com/11c6c02560
    – gelraen
    Feb 4, 2013 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, 2013 at 14:07
  • 3
    @dwwdw if you are on linux (might work elsewhere as well), see man sort, look for KEYDEF.
    – peterph
    Feb 4, 2013 at 14:29
  • 1
    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, 2013 at 18:54

1 Answer 1

2

Just did this test

$ cat tfile
alpha@domain.com,2009-11-26
tack2@domain.com,2009-11-27
overflow@domain2.com,2009-11-28
overflow@domain2.com,2009-11-29
overflow@domain2.com,2009-11-27

Sort on k2 Works!

$ sort -t ',' -k2 tfile 
alpha@domain.com,2009-11-26
overflow@domain2.com,2009-11-27
tack2@domain.com,2009-11-27
overflow@domain2.com,2009-11-28
overflow@domain2.com,2009-11-29

Sort on k2 with unique Works !

$ sort -t ',' -k2 tfile -u
alpha@domain.com,2009-11-26
tack2@domain.com,2009-11-27
overflow@domain2.com,2009-11-28
overflow@domain2.com,2009-11-29

sort of rogue key - Only prints first line

$ sort -t ',' -k3 tfile -u
alpha@domain.com,2009-11-26
$ 

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

2
  • 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, 2013 at 17:16
  • 1
    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, 2013 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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