# sort command...

**URL:** <https://community.unix.com/t/sort-command/177384>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [November 5, 2007, 4:50am UTC](https://community.unix.com/t/sort-command/177384 "2007-11-05T04:50:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Indalecio](https://community.unix.com/letter_avatar/indalecio/32/5_5575768a8748004e209b776fc1b2916d.png) [@Indalecio](https://community.unix.com/u/Indalecio)\
**Post date:** [November 5, 2007, 4:50am UTC](https://community.unix.com/t/sort-command/177384/1 "2007-11-05T04:50:42Z")

</div>

Hi

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

```nohighlight
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 😕

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

---

<div class="post-metadata">

**Author:** ![sysgate](https://community.unix.com/user_avatar/community.unix.com/sysgate/32/3504_2.png) [@sysgate](https://community.unix.com/u/sysgate)\
**Post date:** [November 5, 2007, 7:28am UTC](https://community.unix.com/t/sort-command/177384/2 "2007-11-05T07:28:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Indalecio](https://community.unix.com/letter_avatar/indalecio/32/5_5575768a8748004e209b776fc1b2916d.png) [@Indalecio](https://community.unix.com/u/Indalecio)\
**Post date:** [November 5, 2007, 7:34am UTC](https://community.unix.com/t/sort-command/177384/3 "2007-11-05T07:34:11Z")

</div>

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.

```nohighlight
  -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

```

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [November 5, 2007, 7:48am UTC](https://community.unix.com/t/sort-command/177384/4 "2007-11-05T07:48:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Indalecio](https://community.unix.com/letter_avatar/indalecio/32/5_5575768a8748004e209b776fc1b2916d.png) [@Indalecio](https://community.unix.com/u/Indalecio)\
**Post date:** [November 5, 2007, 7:52am UTC](https://community.unix.com/t/sort-command/177384/5 "2007-11-05T07:52:16Z")

</div>

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:

```nohighlight
  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.

```

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [November 5, 2007, 8:17am UTC](https://community.unix.com/t/sort-command/177384/6 "2007-11-05T08:17:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Indalecio](https://community.unix.com/letter_avatar/indalecio/32/5_5575768a8748004e209b776fc1b2916d.png) [@Indalecio](https://community.unix.com/u/Indalecio)\
**Post date:** [November 5, 2007, 8:19am UTC](https://community.unix.com/t/sort-command/177384/7 "2007-11-05T08:19:30Z")

</div>

This makes sense, cheers drl

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [November 5, 2007, 8:56am UTC](https://community.unix.com/t/sort-command/177384/8 "2007-11-05T08:56:13Z")

</div>

> [@](#):
>
> sort -u file | sort -n gives my expected result. I could also use sort -u file | uniq .

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

---

<div class="post-metadata">

**Author:** ![Indalecio](https://community.unix.com/letter_avatar/indalecio/32/5_5575768a8748004e209b776fc1b2916d.png) [@Indalecio](https://community.unix.com/u/Indalecio)\
**Post date:** [November 5, 2007, 8:58am UTC](https://community.unix.com/t/sort-command/177384/9 "2007-11-05T08:58:15Z")

</div>

> [@matrixmadhan](#):
>
> You don't have to use ' uniq ' when using sort -u file.

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

Cheers for the spot
