That is the way all of the UNIX and Linux sort utilities work. This is clearly documented on the POSIX sort man page:
Quote:
When there are multiple key fields, later keys are
compared only after all earlier keys compare equal.
Except when the -u option is specified, lines that
otherwise compare equal are ordered as if none of
the options -d, -f, -i, -n or -k were present (but
with -r still in effect, if it was specified) and
with all bytes in the lines significant to the com-
parison.
Unfortunately, this is not mentioned on the BSD, OS/X, and Linux man pages available in the Man Pages section of this site.
This User Gave Thanks to Don Cragun For This Post:
hmm....The output you posted is in fact sorted on the second field. Notice the penultimate line with "50034", which moved up in the output.
Still, there are only two distinct keys in your input and the question arises how sort should handle lines with identical keys. If you don't want the lines with identical second fields to be sorted upon the contents of the first field (or in a different manner, like reversed) you should define a compund sort key based on several fields.
Take your second sort key for example: it says the lines should be sorted upon a key composed from a string starting at the first character of the second field to the 6th character of the second field. Therefore, the sorting routine of the "sort" command "sees" these keys (i include line numbers for reference):
How is it supposed to determine, if, for instance, line 2 should go before or after line 3? This is why the file seems to be sorted based on field 1, but this is only the case because so many contents of your specified key are identical and for these the default sort (start of line) kicks in.
If you want to override this define a sort key which leads to distinct sort key contents.
Tip: In your "-k ..." definition you don't have to keep the line order. You may specify for instance "first field 2, then field 4, only then field 3" as well.
Hi All
I have a requirement to list all the files in chronological order based on the date value in the file name.For ex if I have three files as given below
ABC_TEST_20160103_1012.txt
ABC_TEST_20160229_1112.txt
ABC_TEST_20160229_1112.txt
I have written code as given below to list out... (2 Replies)
Hello ,
I have to write a bash script. I will explain the logic based on a scenario.
Scenario :
Suppose I have few files in a Folder X :
FILE_201508.list
FILE_201510.list
FILE_201507.list
abc_201510.csv
xyz_201508.csv
abc_201507.csv
def_201507.csv
1) Now ,... (3 Replies)
Oracle Enterprise Linux 6
This is my file. Two fields separated by space
$ cat testfile.txt
MARCH9 MARCH4
MARCH1 MARCH5
MARCH2 MARCH326
MARCH821 MARCH7
MARCH6 MARCH2
$
$
The following numeric sort, based on the first field's 6th character works as expected.
$
$ sort -n -k 1.6... (7 Replies)
Hi team,
We have few files landing to our server based on sequence number. These files have to be processed in the sequence number order. Once the sequence number has reached its maximum, the files with sequence number 0000 has to be processed.
For example:
IN9997
IN9998
IN9999
IN0000... (7 Replies)
the below is sorted as it is. the fields that i'm interested in are the 4th and 5th field.
i want to sort the based on the 4th field.
my past attempt to do this was to do something like this:
awk '{print $4}'| awk '{print $1":"$2}' datafile | sort | uniq
however, if i do that, i lose... (2 Replies)
i need to write a bash script that recive a list of varuables
kaka pele ronaldo beckham zidane messi rivaldo gerrard platini
i need the program to print the longest word of the list.
word in the output appears on a separate line and word order in the output is in the order Llachsicografi costs.... (1 Reply)
Hello,
I have a file with multiple entries.
@SFGF-GA2-1_58:5:36:11009:999#0/1
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
+SFGF-GA2-1_58:5:36:11009:999#0/1
################################################################################... (10 Replies)
Hello,
I have data where words are separated by a delimiter. In this case "="
The number of delimiters in a line can vary from 4to 8. The norm is 4.
Is it possible to have a script where the file could be separated starting with highest number of delimiters and ending with the lowest
An... (8 Replies)
Hi,
I have a requirement whereby I have to sort a flat file based on Multiple Columns (similar to ORDER BY Clause of Oracle). I am getting 10 columns in the flat file and I want the file to be sorted on 1st, 3rd, 4th, 7th and 9th columns in ascending order. The flat file is pipe seperated.
Any... (15 Replies)
Hi,
I want a list of entries in 3 space delimited columns. I want to sort entries based on the very first column. Rows can't be changed. For example:
If I have...
Abc Abc Acc
Bca Bda Bdd
Cab Cab Cbc
Dbc Dca Dda
Abc Abc Acc
the output should be...
Abc Abc Acc
Abc Abc Acc
Bca... (7 Replies)