从下面的输出,我想要模式匹配“线程id :从第一组和第二组行输出。将模式存储在两个不同的数组中,并希望比较相同的。
(in)* : 2000:0:0:0:0:0:0:1/2026 -> 4000:0:0:0:0:0:0:1/2026;17, Conn Tag: 0x0, VRF GRP ID: 0(0), If: vms-2/0/0.16392 (4045), CP session Id: 0, CP sess SPU Id: 0, flag: 600023:c0, wsf: 0, diff: 0, FCB: 0
npbit: 0x0 thread id:23, classifier cos: 0, dp: 0, is cos ready: No, sw_nh: 0x0, nh: 0x0, tunnel_info: 0x0, pkts: 36935, bytes: 2807060
usf flags: 0x10, fabric endpoint: 16
pmtu : 9192, tunnel pmtu: 0
(out) : 2000:0:0:0:0:0:0:1/2026 <- 4000:0:0:0:0:0:0:1/2026;17, Conn Tag: 0x0, VRF GRP ID: 0(0), If: vms-2/0/0.0 (20429), CP session Id: 0, CP sess SPU Id: 0, flag: 600022:c0, wsf: 0, diff: 0, FCB: 0
npbit: 0x0 thread id:255, classifier cos: 0, dp: 0, is cos ready: No, sw_nh: 0x0, nh: 0x0, tunnel_info: 0x0, pkts: 0, bytes: 0
usf flags: 0x0, fabric endpoint: 16
pmtu : 9192, tunnel pmtu: 0我编写了如下代码,并在$1中获得了输出,但无法将数字与$1输出进行比较
my $file = '/homes/rageshp/PDT/SPC3/vsp_flow_output1.txt';
open(FH, $file) or die("File $file not found");
while(my $String = <FH>)
{
if($String =~ /thread id:(\d+)/)
{
print "$1 \n";
}
}
close(FH);
my @thrid = $1;
print "$thrid[0]";发布于 2022-07-26 11:04:31
在遍历文件时,变量$1将具有不同的值。如果您试图在循环之后将其值存储在数组中,那么您只会得到最后的值。你需要在循环中用它做点什么。
my @thrids;
while(my $String = <FH>)
{
if($String =~ /thread id:(\d+)/)
{
print "$1 \n";
push @thrids, $1;
}
}
print "@thrids\n";再来几个小提示。
open()的三参数版本。打开(我的$fh,'<',$file)或死(“文件$file未找到”);而(my $String = <$fh>) {. }
$_变量。时间(<$fh>) { if (/thread id:(\d+)/) {. }
$/设置为undef),则可以同时获取所有in。my $file_contents = do { local $/;<$fh> };my @thrids = $file_contents =~ /thread id:(\d+)/g;
发布于 2022-07-26 17:41:04
Perl为这种情况提供了一种方便的方法--从菱形操作符读取,您不需要打开和关闭文件。
一旦读取了与正则表达式匹配的文件,并将其存储在感兴趣的数组中。
一旦从一个文件中读取所有数据,就以方便的方式输出结果,以便进行可视化检查。
use strict;
use warnings;
use feature 'say';
my @thead_ids;
my $re = qr/thread id:(\d+)/;
/$re/ && push @thead_ids, $1 while <>;
say for sort @thead_ids;以script.pl input_file的形式运行
提供的输入数据的输出示例
23
255https://stackoverflow.com/questions/73121894
复制相似问题