- 论坛徽章:
- 0
|
借用redspider大哥的代码,用堆栈...
#!/usr/bin/perl
use strict;
my (@head,@data);
my $stack_point = -1;
open(FILE, 'data.txt') or die $!;
while (<FILE>) {
my @characters = split(//);
for my $c (@characters) {
# 忽略空格或换行, 便于后面匹配
push(@data, $c) if ($c !~ /\s/);
push(@head, $c) if ($stack_point == -1 and $c !~ /\s/);
if ($c eq '(') { $stack_point++ }
elsif ($c eq ')') { $stack_point-- }
# $stack_point 为 0, 且 @head 有数据,表示读完一个记录头
if ($stack_point == 0 and @head) {
my $header = join('',@head);
$header =~ s/=.*$//;
print "$header\t";
@head = (); # 清空头
}
# $stack_point 计数为 -1 说明记录的数据读完
if ($stack_point == -1) {
my $line = join('',@data);
# 匹配并提取指定信息
if ($line =~ /HOST=(\S+)\)\(PORT=(\w+)\).*SERVICE_NAME=(\w+)\)/) {
print "$1 $2 $3\n";
}
@data = (); # 清空数据
}
}
}
close FILE;
|
请各位指教..
[ 本帖最后由 coolend 于 2007-9-4 19:22 编辑 ] |
|