Home > database >  how to read line by line of a given output with pattern match and store the same in two different ar
how to read line by line of a given output with pattern match and store the same in two different ar

Time:07-27

From the below out put i want to pattern match " thread id : from first and second set of line output. store the pattern in two different array and want to compare the same.

   (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

I written the code like below and got the output in $1. but not able to seperate the numbers from the $1 output to compare

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]";

CodePudding user response:

Your variable $1 will have different values as you iterate across the file. If you try to store its value in an array after the loop, then you're only going to get the final value. You need to do something with it inside the loop.

my @thrids;

while(my $String = <FH>)
{
    if($String =~ /thread id:(\d )/)
    {
        print "$1 \n";
        push @thrids, $1;
    }
}

print "@thrids\n";

A few more tips.

  • In modern Perl, we like to use lexical variables as filehandles and the three-argument version of open().

    open(my $fh, '<', $file) or die("File $file not found");
    
    while(my $String = <$fh>)
    {
         ...
    }
    
  • Most Perl programmers would use the implicit $_ variable here.

    while (<$fh>)
    {
        if (/thread id:(\d )/)
        {
            ...
        }
    }
    
  • If you read all of the file in one go (by setting $/ to undef), you can grab all of the IDs at the same time.

    my $file_contents = do { local $/; <$fh> };
    
    my @thrids = $file_contents =~ /thread id:(\d )/g;
    

CodePudding user response:

Perl provides a convenient way for such cases -- read from diamond operator, you do not need to open and close the file.

Once you read the file match against your regular expression and store in an array data of interest.

Once all data from a file read, output the result in a convenient way for visual inspection.

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;

Run as script.pl input_file

Output sample for provided input data

23
255
  •  Tags:  
  • perl
  • Related