perl 我如何从另一个列表中比较并否定该列表中的列表,或者执行if语句来跳过文件中的某些内容

wixjitnu  于 9个月前  发布在  Perl
关注(0)|答案(1)|浏览(73)

我有2个文件,其中一个有这些项目,另一个文件调用比较有1个是“SF-阿雷西博”,我想跳过这个程序。
Bielsko-Biala-PL-Cluster SF-Bonn-German-Cluster SF-Bosconero-Cluster SF-Brampton-CA-Cluster CLEOHECLABCP1 SF-Arecibo SF-Dublin-IE-Cluster SF-JiningCN-Cluster SF-KingsMountainNC-Cluster SF-Lenexa-KS-US SF-New-York-US SF-Rockville-MD-US SF-VanWert-OH-US
`

use feature qw(say);
use strict;
use warnings;

{

    my $file = '/var/log/scripts/clusters_ips.txt';
    my $batch_size = 20;
    my @batches;
    
 {
        open (my $info, '<', $file) or die "Could not open $file: $!";

        chomp(my @clusters = <$info>);
        close $info;
        my @targets;
        @batches = split_into_batches($batch_size, @clusters);
    }
    for my $batch (@batches) {
        my @clusters = @$batch;
        my @targets;
        for my $i (0..$#clusters) {
            my $cluster = $clusters[$i];
        if (@clusters !~ /^SF-Arecibo/){

            my $tind = $i + 1;
            push @targets, "targets.$tind", "\"$cluster\"";

                print  @targets;
                print @clusters;
        }
      ##  system 'mgmt_cli', '-r', 'true', 'install-policy', 'policy-package', '"Site-Firewalls"',
      ## 'access', 'false', 'threat-prevention', 'true', @targets;
}
   }

}
`

字符串

mfuanj7w

mfuanj7w1#

下面是一个如何在批处理中从exceptions_clusters.txt文件中跳过簇的示例。您将这些异常读入一个散列,稍后可以将其传递到split_into_batches() sub中,以从批处理中跳过这些集群:

use feature qw(say);
use strict;
use warnings;

{
    my $file = '/var/log/scripts/clusters_ips.txt';
    my %exceptions = read_exception_clusters('exceptions_clusters.txt');
    my $batch_size = 20;
    my @batches;
    {
        open (my $info, '<', $file) or die "Could not open $file: $!";
        chomp(my @clusters = <$info>);
        close $info;
        my @targets;
        @batches = split_into_batches($batch_size, \%exceptions, @clusters);
    }
    for my $batch (@batches) {
        my @clusters = @$batch;
        my @targets;
        for my $i (0..$#clusters) {
            my $cluster = $clusters[$i];
            my $tind = $i + 1;
            push @targets, "targets.$tind", "\"$cluster\"";
        }
        system 'mgmt_cli', 'install-policy', 'policy-package', '"Site-Firewalls"',
           'access', 'false', 'threat-prevention', 'true', @targets;
    }
}

sub read_exception_clusters {
    my $fn = shift;

    open (my $fh, "<", $fn) or die "Could not open file '$fn': $!";
    chomp(my @exceptions = <$fh>);
    close $fh;
    my %exceptions = map {$_ => 1} @exceptions;
    return %exceptions;
}

sub split_into_batches {
    my $batch_size = shift;
    my $exceptions = shift;
    my @clusters = @_;
    my @batches;
    my $j = 1;
    my $idx = 0;
    for my $cluster (@clusters) {
        next if exists $exceptions->{$cluster};
        if ($j > $batch_size) {
            $j = 1;
            $idx++;
        }
        push @{$batches[$idx]}, $cluster;
        $j++;
    }
    return @batches;
}

字符串

相关问题