sort command...

Hi

I�d like to get a few explanations about how the sort command works when cascading the options.

cscyabl@comet:(develop)> more file
2:A2
2:A1
5:A2
5:A2
10:A1
cscyabl@comet:(develop)> sort -n -u file
2:A1
5:A2
10:A1
cscyabl@comet:(develop)> sort -u -n file
2:A1
5:A2
10:A1
cscyabl@comet:(develop)> sort -u file | sort -n
2:A1
2:A2
5:A2
10:A1
cscyabl@comet:(develop)> sort -n file | sort -u
10:A1
2:A1
2:A2
5:A2

sort -u file | sort -n gives my expected result. I could also use sort -u file | uniq .

The question is what do -u -n or -n -u do?? The -n command seems to work as expected but the "2:A2" gets filtered for some reason :confused:

I've been having some troubles in the past with the sort command when used with the -k option, and I�m a bit fed up now to test 10.000 times what I try to achieve because of me not trusting the results of this damn command :mad:

Anybody knowing about how this all works?
Thanks

"sort" command acts differently in various unix flavors, check the man pages for your distro, and you'll probably find the answer.

Thanks for the reply

"man" gives an explanation on all options taken separately, but nothing I can use to predict the result of a cascaded use of these options. The general consensus is to say that I�ll achieve the -u effects AND the -n effects, but none of the below descriptions is warning me that I will lose my "2:A2" line.

  -u  Suppresses all but one in each set of equal lines (for example, lines
      whose sort keys match exactly).  Ignored characters such as leading
      tabs and spaces, and characters outside of sort keys are not considered
      in this type of comparison.

  -n  Sorts any initial numeric strings (including regular expressions con-
      sisting of optional spaces, optional dashes, and zero (0) or more
      digits with optional radix character and thousands separator, as
      defined by the current locale) by arithmetic value.  An empty digit
      string is treated as zero; leading zeros and signs on zeros do not
      affect ordering.  Only one period (.) can be used in numeric strings.
      All subsequent periods (.) and any character to the right of the period
      (.) will be ignored

Hi.

The behavior pattern that I see is that when you use "-n", the initial part of each line becomes the key. In the sample you provided that key extends from the first character up to the ":". When you also specify the "-u", sort inspects to find that you have 2 sets of identical keys, the set of "2" and the set of "5", so those sets are reduced ... cheers, drl

Thanks for the reply drl

This would explain the output of "sort -n -u", but not of "sort -u -n". Unless it makes no difference in what order you put the options. Again, nothing that "man" seems to highlight in our case:

  The -d, -f, -i, -n, and -r options override the default ordering rules.
  When ordering options appear independent of any key field specifications,
  the requested field ordering rules are applied globally to all sort keys.
  When attached to a specific key (see -k), the specified ordering options
  override all global ordering options for that key.  In the obsolescent
  forms, if one or more of these options follows a +fskip option, it affects
  only the key field specified by that preceding option.

Hi.

I have encountered very few commands where changing the order of the options changes the results. That's supposed to be a feature, advantage, etc. Specifying libraries on an ld command comes to mind, but I'm not so sure I'd count that as "options".

Clearly, the order of the files to be compared on a diff will change the output, but that's the order of the files, not options. On sort, the order of the keys will make a difference, but I would not consider those options like the on / off value of switches such as "-n", "-u", etc.

In general, I think the principle that the order of the options is not important is at a higher level than an individual command ... cheers, drl

This makes sense, cheers drl

You don't have to use ' uniq ' when using sort -u file.

My mistake, I meant "sort -n |uniq"

Cheers for the spot