Misunderstanding of sort behavior


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Misunderstanding of sort behavior
# 1  
Old 10-03-2005
Misunderstanding of sort behavior

Hi everyone,

I use the sort from the 5.3.0 coreutils package. I have a file consisting in 5 fields
separated by a single space, with no leading or trailing blanks.
I want to sort it first according to the 4th field, which contains integers
(some of them negative), and secondly on the second field, which is a string
(i.e. if two lines have the same value for the 4th field I want them sorted according
to the second field).

After reading the man page and the info page I came up with
sort sort -k 4,4n -k 2,2 correspondance_tmp
(where correspondance_tmp is my file), but this seems not to work because
when I ask it to check if the result is sorted according to the 4th field it says
no. So I think I must not know how to specify the fields properly.

Here is a short extract from my working session:
$ sort -k 4,4n -k 2,2 correspondance_tmp > correspondance
$ sort -c -k4,4n correspondance
sort: correspondance:37: disorder: . 0-00.com 1 -1 43
$ cat correspondance | awk '{print $4}' | sort -cn
$

This last check of sortedness exists without error, which means the
corresponding file is correctly sorted.
The line about which sorts complains in file correspondance is in
a large group of lines all having -1 for 4th field, so it seems to be
correctly placed.
All my locales are set to "C".

If anyone has any advice I'd greatly appreciate.
# 2  
Old 10-03-2005
Looks like a bug.
You could try the latest version:
ftp://alpha.gnu.org/gnu/coreutils/co...s-5.90.tar.bz2
A compresssed version of your problematic input file would be useful
# 3  
Old 10-04-2005
OK, problem solved.

It's not a bug, I just misunderstood the behaviour of the -c option of sort:
it does not just check that the file is sorted according to the field specified
by the -k option, it checks that the file is identical to what sort would produce
on the file when sorting it according to this field.
The behavior of sort when two lines have the same value for the specified
field is to compare the whole of these two lines to sort them with respect
to one another. Providing the --stable option causes it instead to leave
them in the order they were in in the original file.
Therefore running sort -c --stable correctly identifies that the file is sorted
according to the desired field in my case.

Here's a short example of this behaviour:
$ cat test_sort
bbb aaa 2
aaa bbb 2
aaa aaa 1
$ sort -k 3,3n -k 2,2 test_sort
aaa aaa 1
bbb aaa 2
aaa bbb 2
$ sort -k 3,3n -k 2,2 test_sort | sort -c -k 3,3n
sort: -:3: disorder: aaa bbb 2
$ sort -k 3,3n -k 2,2 test_sort | sort -c --stable -k 3,3n

This last check is successful.
Thanks for your help, it's the designing of this small example that helped me
understand what the problem was!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

2. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

3. UNIX for Advanced & Expert Users

Weird "sort" behavior

Hi, I'm trying to sort a text file "test": S12 S_S12 S_S1_12 S15 S_N15 S_N1_15 By "sort test", I get: S12 S15 S_N1_15 S_N15 S_S1_12 S_S12 It seems weird: Comparing Line 2 and Line 3, it must be that '-' is bigger than '1'; however, comparing Line 3 and Line 4, it seems that... (3 Replies)
Discussion started by: intermilan
3 Replies

4. AIX

LUN Behavior

Aix 6.1, working with a nim master and nim_altmaster both LPARS have access to the same data LUN, /nimdisk I do realize the risks of having 2 servers access the same LUN, however it serves the purpose of being able to restore mksysb's to/from our DR site if necessary, at least in theory ;) ... (3 Replies)
Discussion started by: mshilling
3 Replies

5. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 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

Echo behavior

Echo is removing extra blank spaces. See the command. export INSTALLDIR=”First Second /Two Space” echo $INSTALLDIR out put: First Second /Two Space Here only on blnak space is present while with command Echo “$INSTALLDIR” Out put: ”First Second /Two Space” It's correct output... (2 Replies)
Discussion started by: Saurabh78
2 Replies

8. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies

9. Shell Programming and Scripting

sed behavior on hp-ux

the sed command: sed 's/^*//' file does not work on HP-UX :-( but it works fine on Linux, content of file: <tab><tab>hello output should be: hello Any ideas?? Thank you Andy (8 Replies)
Discussion started by: andy2000
8 Replies

10. Programming

ls behavior

I put this here because it is a 'behavior' type question.. I seem to remember doing ls .* and getting all the .-files, like .profile .login etc. But ls .* doesn't do that, it lsts the contents of every .*-type subdirectory. Is it supposed to? I should think that a -R should be given to... (10 Replies)
Discussion started by: AtleRamsli
10 Replies
Login or Register to Ask a Question