knx-groupfinder/filter.pl

73 lines
1.3 KiB
Perl
Raw Normal View History

2024-01-28 14:19:12 +00:00
#!/usr/bin/perl
use strict; use warnings;
2024-01-28 14:34:35 +00:00
use Getopt::Long;
my $newlines = '';
my $file = '';
GetOptions ("newlines" => \$newlines, # print newlines
"file=s" => \$file # input file
) or die("Error in command line arguments\n");
2024-01-28 14:19:12 +00:00
my %seen = ();
2024-01-28 14:34:35 +00:00
my $in_file;
if ($file) {
open my $in_file, $file or die "Could not open $file: $!";
while (my $line = <$in_file>) {
last if not defined $line;
chomp $line;
my @l = $line =~ /([0-9]+\/[0-9]+\/[0-9]+)/mg;
foreach my $item (@l) {
$seen{$item}++;
}
}
close $in_file;
} else {
for (;;) {
my $input = <STDIN>;
last if not defined $input;
chomp $input;
my @l = $input =~ /([0-9]+\/[0-9]+\/[0-9]+)/mg;
foreach my $item (@l) {
$seen{$item}++;
}
2024-01-28 14:19:12 +00:00
}
}
my @uniq = sort gw_comp keys %seen;
if (@uniq) {
2024-01-28 14:34:35 +00:00
if ($newlines) {
foreach my $item (@uniq) {
print "$item\n";
}
} else {
print "@uniq\n";
}
2024-01-28 14:19:12 +00:00
}
sub gw_comp{
# extract $1/$2/$3 for both GAs
$a =~ /([0-9]+)\/([0-9]+)\/([0-9]+)/;
my $a1 = $1;
my $a2 = $2;
my $a3 = $3;
$b =~ /([0-9]+)\/([0-9]+)\/([0-9]+)/;
my $b1 = $1;
my $b2 = $2;
my $b3 = $3;
if ($a1 < $b1) {
return -1;
} elsif ($a1 == $b1) {
if ($a2 < $b2) {
return -1;
}
elsif($a2 == $b2) {
return $a3 <=> $b3;
}
}
return 1;
}