Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort 1

Status
Not open for further replies.
Sep 10, 2013
4
US
I have some data

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
and a quick sort:
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
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
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
 


Assuming your source file is "pkgs.txt", try this:
Code:
awk '/^host/{h0=$0;continue}{print h0":"$0} ' pkgs.txt|sort|awk -F':' '{if(h0!=$1){h0=$1;print h0} print $2}'
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 


Correction:
Code:
awk '/^host/{h0=$0;continue}{print h0":"$0} ' pkgs.txt| sort -d |awk -F':' '{if(h0!=$1){h0=$1;print h0} print $2}'
[noevil]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks for your reply, I first tried awk then gawk and get the following error:

# gawk '/^host/{h0=$0;continue}{print h0":"$0}' pkgs.txt |sort -d |awk -F':' '{if(h0!=$1){h0=$1;print h0} print $2}'
gawk: (FILENAME=pkgs.txt FNR=1) fatal: `continue' outside a loop is not allowed
 

Replace continue with "next".
[morning]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top