Thread Datei-Erstellung nach String-Erkennung (34 answers)
Opened by QWERTZ7 at 2011-03-31 08:21

gabimuc
 2011-03-31 20:27
#147251 #147251
User since
2010-06-21
33 Artikel
BenutzerIn

user image
2011-03-31T11:09:57 bianca
Du willst ja pro Switch eine Datei anlegen.

Also baust du eine große Schleife drum, z.B. so:
Code (perl): (dl )
1
2
3
4
5
foreach (@jet_macs) {
  # quatsch, sorry! ($mac2,$switch2) = split / /,$jet_macs[$_];
  ($mac2,$switch2) = split / /,$_; # so ist richtig
  # und hier die Datei anlegen
}


Editiert von bianca: Codekorrektur


das erinnert mich doch stark an könig einauge ;þ
mag zwar nicht der weisheit letzer schluß sein, aber zumindest funzt's wie geplant. und nun überlegt euch mal die antwort auf so fragen wie "wieso hab ich nur eine datei, wieso hab ich nur einen eintrag in der datei etc pp blabla

Code (perl): (dl )
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
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $file = 'relevante_macs.txt';
my %switch_macs;

# Open file with previously grepped data
open (my $in, '<', $file) or die "Can't open $file: $!";
my @jet_macs = <$in>;

# Build a hash with switches as key and macs as value
foreach my $line (@jet_macs) {
    my ($mac, $switch) = split ' ', $line;
    push @{$switch_macs{$switch}}, $mac; 
}

# What the fuck was this last push statement about?
print Dumper \%switch_macs;

# Now we got a nice hash, lets create the desired files
foreach my $switch (keys %switch_macs) {
    if ( -e "$switch.cfg" ) {
        print "Sorry, won't overwrite an existing file\n";
        next;
    } else {
        my $file_out;
        open($file_out, '>', "$switch.cfg") or die "Can't open $file_out: $!";

        print $file_out "Here comes the header\n";
        # Put the macs into the file
        foreach my $mac ( @{$switch_macs{$switch}} ) {
            print $file_out "show multiauth mac $mac\n";        
        }
        print $file_out "Here comes the footer\n";

        close $file_out;
    }
}

Last edited: 2011-03-31 22:49:27 +0200 (CEST)
root is a state of mind - k0resh

View full thread Datei-Erstellung nach String-Erkennung