I have a script that outputs file paths (via find
), which I want to sort based on very specific custom logic:
1st sort key: I want the 2nd and, if present, the 3rd
-
-separated field to be sorted using custom ordering based on a list of keys I supply - but excluding a numerical suffix.
With the sample input below, the list of keys is:
rp,alpha,beta-ri,beta-rs,RC
2nd sort key: numeric sorting by the trailing number on each line.
Given the following sample input (note that the /foo/bar/test/example/8.2.4.0
prefix of each line is incidental):
/foo/bar/test/example/8.2.4.0-RC10
/foo/bar/test/example/8.2.4.0-RC2
/foo/bar/test/example/8.2.4.0-RC1
/foo/bar/test/example/8.2.4.0-alpha10
/foo/bar/test/example/8.2.4.0-beta-ri10
/foo/bar/test/example/8.2.4.0-beta-ri2
/foo/bar/test/example/8.2.4.0-beta-rs10
/foo/bar/test/example/8.2.4.0-beta-rs2
/foo/bar/test/example/8.2.4.0-alpha2
/foo/bar/test/example/8.2.4.0-rp10
/foo/bar/test/example/8.2.4.0-rp2
I expect:
/foo/bar/test/example/8.2.4.0-rp2
/foo/bar/test/example/8.2.4.0-rp10
/foo/bar/test/example/8.2.4.0-alpha2
/foo/bar/test/example/8.2.4.0-alpha10
/foo/bar/test/example/8.2.4.0-beta-ri2
/foo/bar/test/example/8.2.4.0-beta-ri10
/foo/bar/test/example/8.2.4.0-beta-rs2
/foo/bar/test/example/8.2.4.0-beta-rs10
/foo/bar/test/example/8.2.4.0-RC1
/foo/bar/test/example/8.2.4.0-RC2
/foo/bar/test/example/8.2.4.0-RC10
I found out a solution totally different of what @mklement0 suggests me.
Content of outfile2.txt :
The only thing wrong with this is that
alpha10
came beforealpha8
Any clue ?
Using a variant of my answer to your original question:
./your-script
represents whatever command produces the output you want to sort.Note that an aux. character,
|
, is used to facilitate sorting, and the assumption is that this character doesn't appear in the input - which should be reasonable safe, given that filesystem paths usually don't contain pipe characters.Any field 2 values (sans numeric suffix) that aren't in the list of sort keys, sort after the field 2/3 values that are, using alphabetic sorting among them.
While this does not match what the OP is looking for, it would be useful to point out that
sort
command has an option-V
for version sorting. And it does the job by following correct order of characters in ASCII table (i.e. UPPERCASE letters first, lowercase letters next)For example:
And sorting:
So, it is useful to be aware of this when giving version names.
With that said, if you insisted, this is one liner that use
sed
to enforce sorting: