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?
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.873I 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.393Your 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.9571The
-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