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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort an Associative Array 1

Status
Not open for further replies.

Einstein47

Programmer
Nov 29, 2001
737
US
I have a short awk script that parses my web server log for "404" errors and displays which files weren't found as well as a count of the number of times the reference occurred. However the output is very random (that's what you get with associative arrays).

Does anybody know of an easy way to sort associative arrays? Can I implement a system call to "/usr/bin/sort" and have that sort my array for me?

Any suggestions are appreciated. Einstein47
(How come we never see the headline, "Psychic Wins Lottery"?)
 
Here is my current script if anyone is interested:
Code:
BEGIN {
  FS = "\""
  title = "HTTP sslaccess_log:\n"
  flag = 0
  error = 0
  site = 0
  separator = ""
  allERRORS = ""
}
$3 ~ / 404 / {
  if ( $2 ~ /site/ ) {
    split( $2, name, " " )
    if ( index( allERRORS, name[2]) == 0 )
    {
      allERRORS = allERRORS separator name[2]
      separator = ","
    }
    arrCOUNT[name[2]]++
    site=1
  }
  else {
    print title " - " $0
    title = ""
    error++
  }
  flag++
}
END {
  if (flag > 0) {
    if (error > 0) {
      printf("---- %3d: basic errors\n", error)
    }
    if (site = 1)
    {
      split( allERRORS, arrERRORS, "," )
      for ( inx in arrERRORS )
      {
        printf("---- %3d: %s\n",arrCOUNT[arrERRORS[inx]],arrERRORS[inx])
      }
    }
    printf("---- %3d: Total 404 Errors\n",flag)
  }
}
Einstein47
(How come we never see the headline, "Psychic Wins Lottery"?)
 
Awk has the built in asort() function which you can use.
However with the way that you are using subscripts here
I don't know how(or why ;)) it will work.
Here is a copy of a qsort function I use as well:

function qsortA(array,min,max) {
if (min > max) {
return
}

m = min
for (i = (min + 1) ; i <= max ; i++) {
if (array < array[min]) {
swap(array,++m,i)
swap(array,min,m)
}
}
qsortA(array,min,(m - 1))
qsortA(array,(m + 1),max)
}

function swap(array,ind1,ind2) {
sw = array[ind1]
array[ind1] = array[ind2]
array[ind2] = sw
}

Another option would be:
You could certainly save the output of the script in
a shell variable and pipe it through sort.

ssl_log_info=$(awk -f scriptname | sort)

HTH
MMD
 
Thank you very much - I was thinking that I would need to copy the elements of this array to a standard array and do some kind of sort on them. My output isn't large enough to warrant a quick-sort, but I will keep your code when the opportunity presents itself [smarty]

The second suggestion isn't possible because this awk script is a part of a much larger log checking script which browses many different logs for many different things. When errors are found I get an email. It is pretty nice to have a script look at all the logs for you.

I'll try recoding my associative array into a more &quot;standard&quot; array and then sort that. Thanks again [medal] Einstein47
(How come we never see the headline, &quot;Psychic Wins Lottery&quot;?)
 
I think you need to write:

Code:
for(inx in arrERRORS){
  printf(&quot;---- %3d: %s\n&quot;,arrCOUNT[arrERRORS[inx]],
         arrERRORS   [inx])
Code:
| &quot;sort -f -k2&quot;
Code:
}

This will pipe your print through sort -- use -k(n)
to sort on the right position (see man sort).

jmagave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top