1

I have an unsorted server list like the following;

bgsqlnp-z101
bgsqlnp-z102
bgsqlnp-z103
bgsqlnp-z2
bgsqlnp-z3
bgsqlnp-z5
dfsqlnp-z108
dfsqlnp-z4
bgsqlnp-z1
dfsqlprd-z8
fuqddev-z88
fuqhdev-z8
ghsbqudev-z18
heiappprod-z1
htsybprd-z24

Using sort to read-in the file, I'm trying to get the following;

bgsqlnp-z1
bgsqlnp-z2
bgsqlnp-z3
bgsqlnp-z5
bgsqlnp-z101
bgsqlnp-z102
bgsqlnp-z103
dfsqlnp-z4
dfsqlnp-z108
dfsqlprd-z8
fuqddev-z88
fuqhdev-z8
ghsbqudev-z18
heiappprod-z1
htsybprd-z24

I'm just not able to find the right keydef for my -k option. Here's the closest I've been able to get;

sort -k2n -t"z"

bgsqlnp-z1
bgsqlnp-z101
bgsqlnp-z102
bgsqlnp-z103
bgsqlnp-z2
bgsqlnp-z3
bgsqlnp-z5
dfsqlnp-z108
dfsqlnp-z4
dfsqlprd-z8
fuqddev-z88
fuqhdev-z8
ghsbqudev-z18
heiappprod-z1
htsybprd-z24

The numbers are in the right order, but the server names aren't sorted. Attempts using a multi-field keydef (-k1,2n) seem to have zero effect (i get no sorting at all).

Here's some extra info about the server names; 1) All of them have a "-z[1-200]" suffix on the names, some numbers repeat. 2) Server names are differing lengths (4 to 16 characters) So using 'cut' is out of the question

Signal15
  • 558
  • 5
  • 16
  • 1
    Found out how to do this with Linux! (RHEL5) `sed 's/-z/ /' two | sort -k1,1 -k2g | sed 's/ /-z/'` Hoping there's a way to do it with UNIX (Solaris).... – Signal15 Sep 25 '13 at 17:24
  • the `sed` stuff wrapping will work. Of course solaris uses zero based keys, (/usr/xpg4/bin/sort may grok -k options), like `-0 +1 +1 -2` but I don't recognize the `g` option to the sort key. `man sort` is your friend. Good luck. – shellter Sep 26 '13 at 00:21
  • The `-g` option for `sort` is Linux-specific, means "Dictionary sort". Number show up in the "correct" order (1,2,3,4,30,100) instead of 100,1,2,3,30,4. – Signal15 Sep 26 '13 at 15:47

1 Answers1

1

You can use sed to get around having a multi-character separator. You can switch between numeric and dictionary order after each sort key definition. Note that you have to have multiple -k options for multiple keys, check the man page for details on this.

Something like this:

sed 's/-z/ /' file | sort -k2,2n -k1,1d | sed 's/ /-z/'
idfah
  • 1,328
  • 10
  • 16
  • On UNIX (Solaris 10), that just sorts everything by the second-column (the numbers). :/ – Signal15 Sep 25 '13 at 21:57
  • @Signal15 you are trying to sort by the second column and break ties using the first column, right? – idfah Sep 26 '13 at 01:17
  • @Signal15 I don't have a Solaris machine to test anymore but according to the man page it looks like this should work – idfah Sep 26 '13 at 01:20
  • I'm trying to sort by the first column, then break ties by sorting the second column. Basically, so I get server names alphabetically, then sorted by their zone-numbers. See the middle code-chunk in my question. – Signal15 Sep 26 '13 at 15:45