INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

sort

sort

(OP)
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 

RE: sort



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

RE: sort



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

RE: sort

(OP)
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

RE: sort


Replace continue with "next".
morning

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close