Schrift
Wiki:Tipp zum Debugging: use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper \@var;
[thread]8879[/thread]

Bestimmte zeilen einer datei einlesen: zeilen eytrahieren



<< >> 5 Einträge, 1 Seite
Gast Gast
 2007-03-27 17:33
#75436 #75436
Hallo perl Community,

ich habe eine kleine Frage.
Ich möchte eine Rückantwort eines Befehls auswerten, allerdings nur bestimmte Zeilen zwischen zwei Wörtern.

Die Rückgabe sieht so aus:
/snip
Aggregate 'xyz_a01'

   Total space    WAFL reserve    Snap reserve    Usable space
  7867009024KB     786700900KB             0KB    7080308124KB

Space allocated to volumes in the aggregate

Volume                          Allocated            Used       Guarantee
xyz_root                  52510088KB       1916636KB          volume
xyz_v001                1934948116KB    1617418204KB          volume
xyz_test                  41943092KB      23746348KB          volume
xyzc_v003                1159081868KB     940400392KB          volume


Aggregate                       Allocated            Used           Avail
Total space                  6303522376KB    5245256688KB     770219212KB
Snap reserve                          0KB             0KB             0KB
WAFL reserve                  786700900KB        246580KB     786454320KB
/end

Ich versuche nun nur die Zeilen die nach Volume kommen bis zum Aggregate zu lesen. Also möchte ich nur die namen der Volumes haben.
kann mir wer nen Tipp geben ?
renee
 2007-03-27 17:49
#75437 #75437
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Wie bekommst Du die Antwort? Als String? Als Datei?

Bei String:
Code (perl): (dl )
1
2
3
4
5
6
7
my $string = "Rückantwort";

my @parts = split /\n\n/,$string;
for my $part ( @parts ){
    next unless $part =~ m!^\s*Volume!;
    print $part;
}


Mit Datei:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
my $file = '/path/to/file';

{
    local $/ = "\n\n";
    open my $fh,'<',$file or die $!;
    while( my $entry = <$fh> ){
        next unless $entry =~ m!^\s*Volume!;
        print $entry;
    }
}
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
stephanb
 2007-03-27 18:23
#75438 #75438
User since
2007-03-27
10 Artikel
BenutzerIn
[default_avatar]
Hallo renee,

danke für die schnelle Hilfe.
ich bekomme die Anwort als ausgabestream

Aber irgendwie gibt er mir nur die Zeile Volume aus, ich benötige aber alles zwischen Volume und Aggregate.
Also aus dem Beispiel:
xyz_root 52510088KB 1916636KB volume
xyz_v001 1934948116KB 1617418204KB volume
xyz_test 41943092KB 23746348KB volume
xyzc_v003 1159081868KB 940400392KB volume

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
 open (VOL, "$ssh $filer aggr show_space $aggr |") || die "Cannot connect to $filer: $! \n";
while (<VOL>)
{
chomp (my $r3="$_");
my @parts = split /\n\n/,$r3;
for my $part (@parts)
{
next unless $part =~ m!^\s*Volume!;
print ("Habe: $part\n");
}
#print ("Lese $r3 \n");
} # end while VOL
close (VOL);
renee
 2007-03-27 18:29
#75439 #75439
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Naja, Du hast es also als "Stream". Du kannst es auf zwei unterschiedliche Weisen machen:

Möglichkeit 1:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 open (VOL, "$ssh $filer aggr show_space $aggr |") || die "Cannot connect to $filer: $! \n";
while (my $line = <VOL>){
    if( $line =~ /^\s*Volume/ ){
        $bool = 1;
    }

    next unless $bool;

    print $line;

    if( $line =~ /^\n+$/ ){
        $bool = 0;
    }
} # end while VOL
close (VOL);


Möglichkeit 2:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
 my $string = "";
open (VOL, "$ssh $filer aggr show_space $aggr |") || die "Cannot connect to $filer: $! \n";
while (my $line = <VOL>)
{
    $string .= $line;
} # end while VOL
close (VOL);

my @parts = split /\n\n/,$string;
for my $part (@parts){
     next unless $part =~ m!^\s*Volume!;
     print ("Habe: $part\n");
}
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
stephanb
 2007-03-27 18:37
#75440 #75440
User since
2007-03-27
10 Artikel
BenutzerIn
[default_avatar]
Hallo,

danke schön, das ist genau des was i wollte
<< >> 5 Einträge, 1 Seite



View all threads created 2007-03-27 17:33.