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
我需要收集START
之间找到的所有行并将其写入2个文件。
我尝试使用Perl中的触发器,如:
open(FILE, $ARGV[0]);
while (<FILE>) {
if (/START/ .. /START/) {
print "$. $_ \n";
}
}
但我只得到START
的行。你能帮我吗?
答案 0 :(得分: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
输出:
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
================================================================================
答案 1 :(得分:0)
如果Perl不是必须的,
$ awk -vRS="START" 'NF{ print $0 > ++c".txt" }' file
答案 2 :(得分:0)
您使用START
行作为分隔符,因此您需要使用Perl的$/
(这是一个记录分隔符)来调整一点。
#! /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;
}
如果命令行中没有命名输出,则上面的代码会将记录发送到标准输出。否则,它会将它们全部输出,其中可能有任意数量。
答案 3 :(得分:0)
split 功能会对您有所帮助,例如
#!/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";
}
<强>输出强>:
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