summaryrefslogtreecommitdiff
path: root/tests/ovsdb-monitor-sort.pl
blob: 24f3ffcd6162be80ee4b7f8c0d5349c7415c5fd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/perl

use strict;
use warnings;

# Breaks lines read from <STDIN> into groups using blank lines as
# group separators, then sorts lines within the groups for
# reproducibility.

sub compare_lines {
    my ($a, $b) = @_;

    my $u = '[0-9a-fA-F]';
    my $uuid_re = "${u}{8}-${u}{4}-${u}{4}-${u}{4}-${u}{12}";
    if ($a =~ /^$uuid_re/) {
        if ($b =~ /^$uuid_re/) {
            return substr($a, 36) cmp substr($b, 36);
        } else {
            return 1;
        }
    } elsif ($b =~ /^$uuid_re/) {
        return -1;
    } else {
        return $a cmp $b;
    }
}

sub output_group {
    my (@group) = @_;
    print "$_\n" foreach sort { compare_lines($a, $b) } @group;
}

if ("$^O" eq "msys") {
    $/ = "\r\n";
}
my @group = ();
while (<STDIN>) {
    chomp;
    if ($_ eq '') {
        output_group(@group);
        @group = ();
        print "\n";
    } else {
        if (/^,/ && @group) {
            $group[$#group] .= "\n" . $_;
        } else {
            push(@group, $_);
        }
    }
}

output_group(@group) if @group;