2010-07-29 5 views
1

DÉBUTComment puis-je collecter les lignes entre deux mots clés?

Length: 1432 

    RIdentifier: 4 

    VIdentifier: 4 

    Format: 5 

    TS number: 9 

    DHeader 

    Version  = 1 
    Length   = 1432 
    Command Flags = RPT (0xd0) 
    Command Code = Accounting-Request (271) 
    Application Id = Rf-Application (3) 
    Hop By Hop Id = 51 
    End To End Id = 8847360 

START 

    Length: 12 

    RIdentifier: 2 

    VIdentifier: 4 

    Format: 5 

    TS number: 6 

    DHeader 

    Version  = 1 
    Length   = 1432 
    Command Flags = RPT (0xd0) 
    Command Code = Accounting-Request (271) 
    Application Id = Rf-Application (3) 
    Hop By Hop Id = 51 
    End To End Id = 8847360 

START 

je dois collecter toutes les lignes qui se trouvent entre START et écrire dans 2 fichiers. J'ai essayé avec flip flop en Perl comme:

open(FILE, $ARGV[0]); 
while (<FILE>) { 
    if (/START/ .. /START/) { 
     print "$. $_ \n"; 
    } 
} 

mais je reçois seulement les lignes qui ont START. Pourrais-tu m'aider s'il te plaît?

Répondre

0

Si Perl n'est pas un must,

$ awk -vRS="START" 'NF{ print $0 > ++c".txt" }' file 
+0

Mais je dois en Perl .. Ceci est une partie du script .. – ram

1

...

#!/usr/bin/perl 
use strict; 
use warnings; 

my $output; 
while(<DATA>) { 
    if(/START/) { 
    if(defined $output) { 
     print $output; 
     $output = ''; 
     print "="x80,"\n"; 
    } 
    next; 
    } else { 
    $output .= $_; 
    } 
} 

__DATA__ 
START 

Length: 1432 

RIdentifier: 4 

VIdentifier: 4 

Format: 5 

TS number: 9 

DHeader 

Version  = 1 
Length   = 1432 
Command Flags = RPT (0xd0) 
Command Code = Accounting-Request (271) 
Application Id = Rf-Application (3) 
Hop By Hop Id = 51 
End To End Id = 8847360 

START 

Length: 12 

RIdentifier: 2 

VIdentifier: 4 

Format: 5 

TS number: 6 

DHeader 

Version  = 1 
Length   = 1432 
Command Flags = RPT (0xd0) 
Command Code = Accounting-Request (271) 
Application Id = Rf-Application (3) 
Hop By Hop Id = 51 
End To End Id = 8847360 

START 

sortie:

Length: 1432 

RIdentifier: 4 

VIdentifier: 4 

Format: 5 

TS number: 9 

DHeader 

Version  = 1 
Length   = 1432 
Command Flags = RPT (0xd0) 
Command Code = Accounting-Request (271) 
Application Id = Rf-Application (3) 
Hop By Hop Id = 51 
End To End Id = 8847360 
================================================================================ 

Length: 12 

RIdentifier: 2 

VIdentifier: 4 

Format: 5 

TS number: 6 

DHeader 

Version  = 1 
Length   = 1432 
Command Flags = RPT (0xd0) 
Command Code = Accounting-Request (271) 
Application Id = Rf-Application (3) 
Hop By Hop Id = 51 
End To End Id = 8847360 
================================================================================ 
+0

Aucun mec. Deux mots-clés sont identiques. Là se trouve le problème. Fondamentalement, j'ai besoin d'extraire les lignes qui se trouvent entre deux mêmes mots-clés. – ram

+0

OK, j'ai mis à jour. – Toto

0

Vous utilisez les START lignes comme délimiteurs, de sorte que vous » ll faut tapdance un peu avec $/ de Perl, qui est un record séparateur.

#! /usr/bin/perl 

use warnings; 
use strict; 

sub usage { "Usage: $0 input output ..\n" } 

die usage unless @ARGV >= 1; 
my $input = shift; 

my $pid = open my $out, "|-"; 
die "$0: fork: $!" unless defined $pid; 

if ($pid == 0) { # child 
    if (@ARGV) { 
    open STDOUT, ">", "/dev/null" or warn "$0: open: $!"; 
    } 
    exec "tee", @ARGV or die "$0: exec: $!"; 
} 

$/ = "START\n"; 

open my $in, "<", $input or die "$0: open: $!"; 
my $i = 1; 
while (<$in>) { 
    chomp; 
    next unless /\S/; 
    print $out "$i. $_\n"; 
    ++$i; 
} 

En l'absence de sorties nommées sur la ligne de commande, le code ci-dessus envoie les enregistrements à la sortie standard. Sinon, il les sort de toutes les sorties, dont il peut y avoir arbitrairement beaucoup.

0

split fonction vous aidera, comme

#!/usr/bin/perl 
    use strict; 
    use warning; 
    open(IN, '<', $ARGV[0]) or die $!; 
    read(IN, my $data, -s $ARGV[0]); 
    my @test = split(/START/, $data); 
    shift(@test); #removing first element of array 
    foreach my $test(@test){ 
    print"Records:$test\n"; 
    } 

SORTIE:

Records: 
    Length: 1432 

     RIdentifier: 4 

     VIdentifier: 4 

     Format: 5 

     TS number: 9 

     DHeader 

     Version  = 1 
     Length   = 1432 
     Command Flags = RPT (0xd0) 
     Command Code = Accounting-Request (271) 
     Application Id = Rf-Application (3) 
     Hop By Hop Id = 51 
     End To End Id = 8847360 


Records: 
    Length: 12 

     RIdentifier: 2 

     VIdentifier: 4 

     Format: 5 

     TS number: 6 

     DHeader 

     Version  = 1 
     Length   = 1432 
     Command Flags = RPT (0xd0) 
     Command Code = Accounting-Request (271) 
     Application Id = Rf-Application (3) 
     Hop By Hop Id = 51 
     End To End Id = 8847360 
Questions connexes