#!/usr/bin/awk -f # @(#) sortaddr.awk 1.0 92/08/05 # sortaddr: sort address file(s) containing records separated by # lines consisting solely of '+'. # Records are sorted based on the lexical value of the entire record. # 92/08/05 john h. dubois iii (john@armory.com) BEGIN { RS = "+" i = 0 } { if ($0 != "\n" && $0 != "") Elem[++i] = $0 } END { qiksort(Elem,1,i) printf "+" for (j = 1; j <= i; j++) printf "%s+",Elem[j] print "" } func qiksort(arr,start,end,left,right,sepval,tmp) { # handle two-element case explicitely for a tiny speedup if ((start - end) == 1) { if ((tmps = arr[start]) > (tmpe = arr[end])) { arr[start] = tmpe arr[end] = tmps } return } left = start; right = end; sepval = arr[int((left + right) / 2)] while (left < right) { while (arr[left] < sepval) left++ while (arr[right] > sepval) right-- if (left <= right) { tmp = arr[left] arr[left++] = arr[right] arr[right--] = tmp } } if (start < right) qiksort(arr,start,right) if (left < end) qiksort(arr,left,end) }