tags:

views:

89

answers:

4

Hi, I have a file like:

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

I need to collect all the lines that are found between START and write it into 2 files. I tried with flip flop in Perl like:

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

But I am getting only the lines that have START. Could you please help me?

A: 

If Perl is not a must,

$ awk -vRS="START" 'NF{ print $0 > ++c".txt" }' file
ghostdog74
But I need in perl.. This is a part of the script..
ram
+1  A: 

...

#!/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

output:

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
================================================================================
M42
No dude. Two Keywords are same. There lies the problem. Basically i need to extract the lines that are found between two same keywords.
ram
OK, i've updated.
M42
A: 

You're using the START lines as delimiters, so you'll need to tapdance a little with Perl's $/, which is a record separator.

#! /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;
}

With no outputs named on the command line, the code above sends the records to the standard output. Otherwise, it fans them out all the outputs, of which there can be arbitrarily many.

Greg Bacon
A: 

split function will help you, like

    #!/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";
    }

OUTPUT:

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
Nikhil Jain