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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| use strict;
use warnings;
open my $RULE, "<", "rule.txt" or die "Can't open rule.txt for reading: $!";
binmode $RULE, ":encoding(utf-16):crlf";
my %rule;
<$RULE>; # Ignore the first line
while (defined(my $rule = <$RULE>)) {
chomp($rule);
my ($source, $destination) = (split /\t/, $rule)[5,7];
foreach my $address_field (grep defined, $source, $destination) {
foreach my $address (grep $_, split /\s+/, $address_field) {
$rule{$address}++;
}
}
}
open (my $ADDRESSGROUP,"<", "addressgroup.txt") or die "Can't open addressgroup.txt for reading: $!";
binmode $ADDRESSGROUP, ":encoding(utf-16):crlf";
open (my $FICHIERGROUP,">", "fichiergroup.txt") or die "Can't open fichiergroup.txt for writing: $!";
binmode $FICHIERGROUP, ":encoding(utf-16):crlf";
print { $FICHIERGROUP } scalar(<$ADDRESSGROUP>);
#trouver tous les groupes d'adresses qui sont dans RULE et les stocker dans FICHIERGROUP
while (defined(my $addressgroup = <$ADDRESSGROUP>)) {
chomp($addressgroup);
my ($out2, $out3) = (split /\t/, $addressgroup)[0,1];
if (defined $out2) {
print $out2, "\n";
if (exists $rule{$out2}){
print $out2, "\n"; # WHY WRITING AGAIN out2 ???
if (defined $out3) {
my @addresses = grep length, split /\s+/, $out3;
print { $FICHIERGROUP } map "$_\n", @addresses;
# add these group addresses to the known rules
$rule{$_}++ foreach @addresses;
}
else {
print "No zone found at line $. in addressgroup.txt [$addressgroup]\n";
}
}
}
else {
print "No group found at line $. in addressgroup.txt [$addressgroup]";
}
}
undef $ADDRESSGROUP;
undef $FICHIERGROUP;
open (my $ADDRESS, "<", "address.txt") or die "can't open address.txt for reading: $!";
binmode $ADDRESS, ":encoding(utf-16):crlf";
open (my $FICHIER1,">", "addressuseless.txt") or die "Can't open addressuseless.txt for writing: $!";
binmode $FICHIER1, ":encoding(utf-16):crlf";
print { $FICHIER1 } scalar(<$ADDRESS>); # Read and write the first line
while (defined(my $address = <$ADDRESS>)) {#trouver toutes les adresses de ADDRESS qui ne sont pas dans RULE et les stocker dans FICHIER1
chomp($address);
if (defined(my $out = (split /\t/, $address)[0])) {
if (exists $rule{$out}) {
print { $FICHIER1 } "address found \n"; # à supprimer par la suite, n'est là que pour savoir si la condition passe ou pas
}
else{
print $out, "\n";
print { $FICHIER1 } $out, "\n" ;
}
}
else {
print "No address found at line $. [$address]\n";
}
} |
Partager