bugGNU Core Utilities - Bugs: bug #20883, sort -u -n drops more lines than...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #20883: sort -u -n drops more lines than expected

Submitted by:  None
Submitted on:  Fri 24 Aug 2007 05:52:13 AM UTC  
 
Category: NoneSeverity: 3 - Normal
Item Group: NoneStatus: None
Privacy: PublicAssigned to: Bob Proulx <rwp>
Open/Closed: Closed

Fri 24 Aug 2007 07:32:13 AM UTC, comment #1:

Thanks for the report but this is not a bug but simply a misunderstanding of what sort is using as a sort key. Note that since this is an anonymous bug posting I cannot respond other than by posting a response to the bug.

By default sort will consider the entire line as the sort key. When using the -n numeric sort flag this is still true. The reason sort -u is collapsing lines is because the sort key cause sort to believe the lines are equivalent. Unless -k is specified to restrict the sort key the default key is the entire line.

The way to sort that input numerically on two different fields is to use the -k option to select both fields. Try this:

sort -k1,1n -k2,2n -u
1 1
1 2
1 3
2 1
3 1

That will provide the result that you are looking to achieve.

Bob Proulx <rwp>
Project AdministratorIn charge of this item.
Fri 24 Aug 2007 05:52:13 AM UTC, original submission:

Given the following example input:

1 3
2 1
1 1
3 1
1 2
1 1

sort -u gives the expected output:

1 1
1 2
1 3
2 1
3 1

But sort -n -u drops all lines that match the sort column:

1 3
2 1
3 1

Or compare sort -n -u -k2:

2 1
1 2
1 3

It's not clear from the info page that this is the correct behavior, but I would expect sort -n -u to do the same as sort -n | uniq, or for this omitting equal keys (instead of equal lines) to be more explicit in the documentation.

Anonymous

 

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by rwp (Posted a comment)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 2 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Fri 24 Aug 2007 07:32:13 AM UTCrwpAssigned toNone=>rwp
      Open/ClosedOpen=>Closed

    Back to the top


    Powered by Savane 3.1-cleanup1