sort command...

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sort command...
# 1  
Old 11-05-2007
Question sort command...


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

cscyabl@comet:(develop)> more file
cscyabl@comet:(develop)> sort -n -u file
cscyabl@comet:(develop)> sort -u -n file
cscyabl@comet:(develop)> sort -u file | sort -n
cscyabl@comet:(develop)> sort -n file | sort -u

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 Smilie

I've been having some troubles in the past with the sort command when used with the -k option, and Im 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 Smilie

Anybody knowing about how this all works?
# 2  
Old 11-05-2007
"sort" command acts differently in various unix flavors, check the man pages for your distro, and you'll probably find the answer.
# 3  
Old 11-05-2007
CPU & Memory

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 Ill 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

# 4  
Old 11-05-2007

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
# 5  
Old 11-05-2007
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.

# 6  
Old 11-05-2007

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
# 7  
Old 11-05-2007
This makes sense, cheers drl
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort Command

Hi All, I have used sort -k1 -n data.txt > output.txt command on a large text data file with over 1,000,000 rows. The command managed to sort the data but the code did not read data according to sequence of occurrence. Given below are the first five lines of the data I need to sort; 1 1... (2 Replies)
Discussion started by: Theo Score
2 Replies

2. Shell Programming and Scripting

Help to sort out... Possible use of sort command

I have an input like (5 Replies)
Discussion started by: dnam9917
5 Replies

3. Shell Programming and Scripting

Help with sort command

I have a file with the following content:- 181268525,0640613864,B,113,22-dec-2011 14:12:08, 181268525,0640613864,C,113,25-dec-2011 14:18:50, 181268525,0640613864,L,113,26-dec-2011 14:07:46, 181268525,0640613864,X,113,01-jan-2012 16:57:45, 181268525,0640613864,X,113,04-jan-2012 14:13:27,... (3 Replies)
Discussion started by: Yoda
3 Replies

4. Shell Programming and Scripting

Is it Possible to sort a list of hexadecimal numbers using "sort" command?

Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies

5. Homework & Coursework Questions

Sort command

I have file ipaddress.txt I want to sort as So what sort command do I have to use. (1 Reply)
Discussion started by: RiderOnsky
1 Replies

6. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

7. Shell Programming and Scripting

Need help with sort command

Hi i have a file containing ip addresses and want to sort those IP addresses in the ascending order. file (match.txt) contents are: i tried: sort -n match.txt output is :... (3 Replies)
Discussion started by: manmeet
3 Replies

8. UNIX for Dummies Questions & Answers

sort command

Hi, I am going to sort a huge flat file using sort command, this file is about 36 million lines, 179 fields delimitered by Ctrl B (002). eg. 1^B198709..... 17^B200301.... 3^B196511.... ..... I want this file being sorted by the first field, the result is like : 1^B198709........ (2 Replies)
Discussion started by: xli
2 Replies

9. UNIX for Dummies Questions & Answers

Help with the Sort command

Can someone please tell me how to sort a file, based on a particular position within the file? I have a line sequential file that is 152 bytes per record, in which i need to sort the file based on the numeric data in positions 142-152. I have done the "man sort" command and see the -k option... (1 Reply)
Discussion started by: rjjenkin
1 Replies
Login or Register to Ask a Question

Featured Tech Videos