Page History

Turn Off History

TJ's simple scripts

sort_ads.pl

A script to sort a collection of classads by key without reordering the ads. What it actually does is accumulate lines from stdin until it gets to a blank line, then prints out those lines sorted and clears the collection.
sort_ads.pl
#!/usr/bin/env perl
use strict;
use warnings;

my %ad;

for my $line ( <STDIN> ) {
  if ($line =~ /^\s*$/) {
    # print out the previous add sorted by key then reset the hash for a new ad.
    if (%ad) {
       for (sort keys %ad) { print $_; }
       undef %ad;
    }
  }
  $ad{$line} = 1;
}

# print the last ad sorted by key
if (%ad) {
   for (sort keys %ad) { print $_; }
   undef %ad;
}
Example
condor_status -long | perl sort_ads.pl