As a matter of fact: yes. You have already been helped and don't need sort any more for this, but it is perhaps beneficial to understand it because you sure will need it next time. So here it goes:
Per defaut sort
sorts alphabetically, not numerical: that means "100" comes before "99", because "1" is "earlier" in the ASCII code than "9". Since you requested a reverse sort ( sort -r
) you got the "last" as first, which was "944". Here is an example that shows the problem: consider following input file:
1
2
3
4
5
6
7
8
9
10
11
Now sort it:
$ sort /input/file
1
10
11
2
3
4
5
6
7
8
9
You need to tell sort
to sort numerically to get the expected result:
$ sort -n /input/file
1
2
3
4
5
6
7
8
9
10
11
But in your case only a part of the field to sort for is numeric, therefor you need to use a "key-definition". sort
treats input as "records" (=lines), which consist of "fields", separated by field separators. The usual field separator is whitespace and the "fields" resemble therefore "words".
We need to explain to sort
that only a certain part of the field is relevant for the sorting. The easiest way is to split the lines at the "_", so we use this as a field separator:
sort -t'_'
Notice this also takes care of the leading space which could have posed a problem if we would rely on whitespace. Now we have two fields in each line, before and after the underscore. We now need to tell sort
that only the first 4 digits/characters in this field are relevant for the sorting (it doesn't matter that some numbers only have 3 digits, sort
will throw characters out in this case). Key definitions are done that way:
-k <field>.<start-character>,<field>.<end-character><options>
Hence:
sort -t'_' -k 2.1,2.4n /path/to/input
Now, we are only interested in certain lines in the file, so we need to filter these out beforehand. And we need to reverse the sort too:
grep "^outbox\/logs\/Client_[0-9]*\.log" /path/to/input | sort -t'_' -k 2.1,2.4rn
I hope this helps.
bakunin