Reputation: 7959
Sort command not working properly when using comma as field separator:
File:
cat /tmp/2.csv
66.199.199.221,115645,0
207.233.77.147,120167,0
204.38.48.1,125767,0
83.144.97.50,127944,3
12.174.177.15,134080,0
195.76.177.90,138124,0
50.202.17.163,162618,0
66.64.209.30,163729,0
40.76.63.140,181976,0
207.241.237.163,2226,1854
Observe line 207.241.237.163,2226,1854
:
Sorting:
sort -t, -nk 2 /tmp/2.csv
66.199.199.221,115645,0
207.233.77.147,120167,0
204.38.48.1,125767,0
83.144.97.50,127944,3
12.174.177.15,134080,0
195.76.177.90,138124,0
50.202.17.163,162618,0
66.64.209.30,163729,0
40.76.63.140,181976,0
207.241.237.163,2226,1854
But it works fine when using TSV:
sort -nk 2 /tmp/3.tsv
207.241.237.163 2226 1854
66.199.199.221 115645 0
207.233.77.147 120167 0
204.38.48.1 125767 0
83.144.97.50 127944 3
12.174.177.15 134080 0
195.76.177.90 138124 0
50.202.17.163 162618 0
66.64.209.30 163729 0
40.76.63.140 181976 0
Tested with different versions of sort:
sort --version
sort (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.
And :
sort (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.
Upvotes: 3
Views: 279
Reputation: 41987
You need to set the key for only the second field i.e. -k2,2
, otherwise if you use -k2
all fields starting from second till end are treated as keys.
Working one :
$ sort -t, -k2,2n file.txt
207.241.237.163,2226,1854
66.199.199.221,115645,0
207.233.77.147,120167,0
204.38.48.1,125767,0
83.144.97.50,127944,3
12.174.177.15,134080,0
195.76.177.90,138124,0
50.202.17.163,162618,0
66.64.209.30,163729,0
40.76.63.140,181976,0
Non-working one :
$ sort -t, -k2n file.txt
66.199.199.221,115645,0
207.233.77.147,120167,0
204.38.48.1,125767,0
83.144.97.50,127944,3
12.174.177.15,134080,0
195.76.177.90,138124,0
50.202.17.163,162618,0
66.64.209.30,163729,0
40.76.63.140,181976,0
207.241.237.163,2226,1854
Upvotes: 4