6

Running the sort command on the first field of a file sort -k1,1 file.txt like this:

1,2,3
2,1,1
10,2,1

gives me:

1,2,3
10,2,1
2,1,1

instead of:

1,2,3
2,1,1
10,2,1

I don't want 10 before 2. Is there any way to get sort to do that?

  • 1
    @user110327 Only with GNU sort. Numeric sorting on the other hand is POSIX. – Satō Katsura May 4 '17 at 11:03
  • @SatoKatsura Could you please explain a bit? I am pretty new to Unix OSs. – user110327 May 4 '17 at 11:04
  • 1
    @user110327 Only GNU sort has option -V. Traditional implementations of sort don't have it. – Satō Katsura May 4 '17 at 11:05
  • 1
    Just a note, but asking for 'human-like' sorting is a little ambiguous, as (at least) GNU sort has a '-h' option for 'human numeric sort' so that sorting happens where 1K < 1M < 1G etc. – einonm May 4 '17 at 12:16
  • @einonm I know it's ambiguous. Hence the apology and the example included. (: – user110327 May 4 '17 at 12:46
10

As explained in man sort:

   -n, --numeric-sort
          compare according to string numerical value

So you want:

$ sort -nk1,1 file
1,2,3
2,1,1
10,2,1

Also note that by default, fields are blank delimited, so those lines in that file have only one field. For instance, the first field of the first line is 1,2,3, not 1. You'd need to add -t , for ,-separated fields:

sort -t, -nk1,1 file

With -n, sort only considers the sequence of characters that forms a valid number at the start of the sorting key (ignoring leading blanks). For that first line, without -t,, depending on the sort implementation and the locale, 1,2,3 will be considered either as 1 or as 1.2 (when the user's decimal separator is ,) or 123 (when the user's thousand separator is , and sort ignores any occurrence of it).

  • @Stéphane, thanks for the edit. I was assuming the OP was only showing the first field of their file not that they're trying to sort on the first comma-delimited field. – terdon May 4 '17 at 11:17
  • Even then, 10,2,3 (1023 with the thousand separators removed) would sort before 1,20,3 (1203) in an English locale with GNU sort, which looks like it's not what he would want. – Stéphane Chazelas May 4 '17 at 11:25
  • @StéphaneChazelas ah! Yes, you're quite right. – terdon May 4 '17 at 11:27
1

Leaving this for anyone who stumbles upon this: sort -V -k1,1 file.txt should work.

  • 4
    Note that this is version sort. It is designed to deal with version number like 1.12.2. It is also GNU-specific so will work on Linux but not on non-GNU systems. Why wouldn't you just want to use the normal numerical sort? – terdon May 4 '17 at 11:13
  • @terdon, ITYM GNU/Linux, most Linux-based systems (the bulk of which are not GNU/Linux these days) won't have GNU sort installed. – Stéphane Chazelas May 4 '17 at 11:27
  • 1
    @StéphaneChazelas s/Linux/Desktop Linux/ or, fine, GNU/Linux and keep RMS happy. – terdon May 4 '17 at 11:41
  • 1
    ChromeOS Desktop (also Linux based) for example would probably not have GNU sort either. And there are plenty of devices with GNU sort installed that are not Desktop systems or don't use Linux as they kernel at all. The point is that it has nothing to do with Linux. Not sure what RMS has to do with that. Other than yes, you'd probably be insulting him and the rest of the GNU community if you implied that their software is Linus' – Stéphane Chazelas May 4 '17 at 11:48

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.