How to UNIX sort by one column only?

48

12

I know that the -k option for the Unix sort allow us to sort by a specific column and all of the following. For instance, given the input file:

2 3
2 2
1 2
2 1
1 1

Using sort -n -k 1, I get an output sorted by the 1st column and then by the 2nd:

1 1
1 2
2 1
2 2
2 3

However, I want to keep the 2nd column ordering, like this:

1 2
1 1
2 3
2 2
2 1

Is this possible with the sort command?

ssn

Posted 2009-08-31T16:20:22.447

Reputation: 583

Answers

68

Give this a try:

sort -s -n -k 1,1

The -s disables 'last-resort' sorting, which sorts on everything that wasn't part of a specified key.

The -k 1 doesn't actually mean "this field and all of the following" in the context of numeric sort, as you can see if you try to sort on the second column. You're merely seeing ties broken by going to the rest of the line. In general, however, you need to specify -k 1,1 to sort only on field one.

Cascabel

Posted 2009-08-31T16:20:22.447

Reputation: 1 240

You are right. This is exactly what I needed. Thanks! – None – 2009-08-31T16:31:53.453

is it possible to use join on the output of this sort? – MiNdFrEaK – 2012-09-27T20:13:58.700

@MiNdFrEaK: The requirement of join is that the input be sorted on the fields you're joining on. So sure, this output is sorted on the first field, and you can join on it. – Cascabel – 2012-09-27T21:49:29.873

I have 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted, so what will be the syntax? will this work? join -j 1 file2.txt sort -s -n -k 1 file1.txt? – MiNdFrEaK – 2012-09-28T04:10:12.220

@MiNdFrEaK: If you have a completely separate question, ask it separately. This question is about sorting a file in a particular way, not about how join works. – Cascabel – 2012-09-28T05:07:56.393

Your answer is correct when numeric sort is used. But it's misleading otherwise. Please update it as follows so nobody gets in any trouble -s -n -k 1,1. – palindrom – 2013-07-18T14:24:39.957

1The -k 1,1 (the ",1" part) doesn't work any better for me. What works is -s -k 1, with -n if you need it. – Totor – 2013-08-08T15:12:32.523

10

To only sort on the first column you should do:

sort -n -s -k1,1

From Unix and Linux System Administration Handbook

sort accepts the key specification -k3 (rather than -k3,3), but it probably doesn’t do what you expect. Without the terminating field number, the sort key continues to the end of the line

tidbeck

Posted 2009-08-31T16:20:22.447

Reputation: 1 365

Not working for me, I need to add the -s option as Cascabel pointed out. – Jean Paul – 2019-01-07T17:08:53.140

@JeanPaul you are right, the documentation for -s says "This option maintains the original record order of records that have an equal key." – tidbeck – 2019-01-09T10:09:55.973

2

None of the provided answers work generally for me.

Both sort -s -k 2 file1 and sort -n -k1,1 do additional sorting with this file:

# cat file1
 3 3 5
 3 2 3
 1 4 7
 0 1 2
 3 2 1

I just had to do this exact thing and ended up using a shell loop. This solution might not work well on a very large file because the entire file needs to be read for each unique value in the sorted column.

Here the file is sorted on column 2 only.

# awk '{print $2}' file1 | sort | uniq | while read index
do  
    awk -v var=$index '$2 == var { print $0}' file1 
done
 0 1 2
 3 2 3
 3 2 1
 3 3 5
 1 4 7

user680341

Posted 2009-08-31T16:20:22.447

Reputation: 21

sort -s -k2,2 file1 – plhn – 2017-02-24T10:22:09.867

The answer proposed by Cascabel is working but I think you missread it. – Jean Paul – 2019-01-07T17:11:42.283