This file can't be sorted by file |sort -k5,5n or any other command.
The only way I've gotten it to sort is by the following:
file |sort -t/ -k5.12n
I am wondering if there is a certain criteria that will cause you to need to use a character numerical sort instead of a column sort? I am sort of confused on this.
In addition, I am also wondering if sort allows you to use the case insensitive option with numerical sorts. What if I had Scratch4 and scratch4 as paths? From what I can see, you can't use the numerical sort and the -f option even if you specify the column and character to use. It will always put the upper case at the top. Is there a way to turn this off through collation or LC_COLLATE?
Since you didn't give sort a -t option, with the data you have above, the command:
cat file| sort -k3,3n
is an inefficient equivalent of the command:
sort file
(There is no third field. And even if you used -t/ , the start of the 3rd field is not numeric.)
This is simple. If you have a string of digits that are not all the same length (same number of digits) and you want to sort them by numeric value (e.g., 5, 25, 100) instead of as a string by collating sequence (i.e., 100, 25, 5), then you need to sort that portion of your input file using a numeric sort instead of using the default alphanumeric sort.
You didn't have any data that would show whether or not a case insensitive sort would matter. "B" comes before "CRA" which comes before "crd" with both case sensitive and case insensitive sorts.
Here is a script that shows a couple of slightly more complex input files, the commands I used to sort them, and the sorted output that seems to match what you're trying to do:
Is this what you were trying to do? Please take another look at the sort man page to figure out why this works. If you still have questions about how the sort keys work in these two sort commands, let me know what doesn't make sense.
Yes I see how it works now. I actually was using a file I copied from somewhere else and the -k3,3n only appeared to work. I was able to get the file to sort through cat file | sort -t/ -k4.15
The other question which you even mentioned is in regard to case sensitivity. Now it seems to be impossible to sort both numerically AND also disregard case, even with the f option. I've tried using different sort and uniq options and if I have a number and a word, it doesn't work. For example:
NEW
/ifs/DumpsterArchive
NEW
/ifs/data
NEW
/ifs/eas_archive
NEW
/ifs/data13
NEW
/ifs/data8
NEW
/ifs/data5
NEW
/ifs/data6
NEW
/ifs/data7
file |sort -t/ -k3.5n
works just great but even with
sort -f -t/ -k3.5n
it still puts Dumpster at the very top of the file.
I therefore tried with
tr [a-z] [A-Z]
but as this would make all the letters upper case then it wouldn't work in the end.
So I tried sed patterns but unless the first letter was the cause of the case sensitive sort every time, I am not sure it would work.
Does sort allow the -f with the -n so that you can sort alphanumerically? If not I assume I could use some form of sed to save the pattern
sort case insensitive on the 1st to the third column from character 4 and then sort numerically using the / as a delimiter and then the third column , fifth character?
Seems close. I will try to review this by checking out the other sort usages you so kindly supplied me with.
The options supplied to sort are applied to the entire line of input and not just parts of the line with the exception of the "-k" option. So the above sort should be read as...
sort the entire file case insensitive due to "-f" while breaking up the entire line into fields as per the forward slash delimiter "-t/"
and first do an ascii sort on fields (start: 1st field; end: 4th character of 3rd field) and finally sort the rest of the line numerically.