gunghopatriot
MIS
I have some data
This is just a sampling of tens of hosts and the output. I've tried a bubble sort:
and a quick sort:
And neither of them will sort it based on the packages.
I don't really care if the hosts are sorted alphanumerically,
but the packages for each host I'd like sorted that way:
Code:
host3719
NK-JDK170_25_32-1.7.0_25-25.i686
NKJAS-JBOSS-ENV-1.0-24.x86_64
NKJAS-JBOSS-EWS_64-2.0.1-3.x86_64
NK-JDK170_25_64-1.7.0_25-25.x86_64
NKSGI-AGENT-JBOSS-1.0-9.x86_64
host7723
NKSGI-AGENT-JBOSS-1.0-9.x86_64
NK-JDK170_25_64-1.7.0_25-25.x86_64
NKJAS-JBOSS-EWS_64-2.0.1-3.x86_64
NKJAS-JBOSS-ENV-1.0-24.x86_64
NK-JDK170_25_32-1.7.0_25-25.i686
host1455
NK-JDK170_25_64-1.7.0_25-25.x86_64
NKSGI-AGENT-JBOSS-1.0-9.x86_64
NK-JDK170_25_32-1.7.0_25-25.i686
NKJAS-JBOSS-ENV-1.0-24.x86_64
NKJAS-JBOSS-EWS_64-2.0.1-3.x86_64
This is just a sampling of tens of hosts and the output. I've tried a bubble sort:
Code:
awk '{ # read every line into an array
line[NR] = $0
}
END { # sort it with bubble sort
do {
haschanged = 0
for(i=1; i < NR; i++) {
if ( line[i] > line[i+1] ) {
t = line[i]
line[i] = line[i+1]
line[i+1] = t
haschanged = 1
}
}
} while ( haschanged == 1 )
# print it
for(i=1; i <= NR; i++) {
print line[i]
}
}' x
Code:
awk 'BEGIN { RS = ""; FS = "\n" }
{ A[NR] = $0 }
END {
qsort(A, 1, NR)
for (i = 1; i <= NR; i++) {
print A[i]
if (i == NR) break
print ""
}
}
function qsort(A, left, right, i, last) {
if (left >= right)
return
swap(A, left, left+int((right-left+1)*rand()))
last = left
for (i = left+1; i <= right; i++)
if (A[i] < A[left])
swap(A, ++last, i)
swap(A, left, last)
qsort(A, left, last-1)
qsort(A, last+1, right)
}
function swap(A, i, j, t) {
t = A[i]; A[i] = A[j]; A[j] = t
}' x
I don't really care if the hosts are sorted alphanumerically,
but the packages for each host I'd like sorted that way:
Code:
host3719
NKJAS-JBOSS-ENV-1.0-24.x86_64
NKJAS-JBOSS-EWS_64-2.0.1-3.x86_64
NK-JDK170_25_32-1.7.0_25-25.i686
NK-JDK170_25_64-1.7.0_25-25.x86_64
NKSGI-AGENT-JBOSS-1.0-9.x86_64
host7723
NKJAS-JBOSS-ENV-1.0-24.x86_64
NKJAS-JBOSS-EWS_64-2.0.1-3.x86_64
NK-JDK170_25_32-1.7.0_25-25.i686
NK-JDK170_25_64-1.7.0_25-25.x86_64
NKSGI-AGENT-JBOSS-1.0-9.x86_64
host1455
NKJAS-JBOSS-ENV-1.0-24.x86_64
NKJAS-JBOSS-EWS_64-2.0.1-3.x86_64
NK-JDK170_25_32-1.7.0_25-25.i686
NK-JDK170_25_64-1.7.0_25-25.x86_64
NKSGI-AGENT-JBOSS-1.0-9.x86_64