in reply to Regex match multiple line output
Hello bartrad,
Something like this?
#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Data::Dumper; while (<>) { chomp; if (/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) { my @tmp = split /\s+/; # print Dumper \@tmp; say $tmp[0]; } } __END__ $ perl test.pl in.txt 1.1.1.1:0
The input format of 'in.txt' is:
====================================================================== +======== LDP Sessions ====================================================================== +======== Peer LDP Id Adj Type State Msg Sent Msg Recv Up Tim +e ---------------------------------------------------------------------- +-------- ---------------------------------------------------------------------- +-------- No Matching Entries Found ====================================================================== +======== ====================================================================== +======== LDP IPv4 Sessions ====================================================================== +======== Peer LDP Id Adj Type State Msg Sent Msg Recv Up Tim +e ---------------------------------------------------------------------- +-------- 1.1.1.1:0 Targeted Established 822443 822431 273d 15:02:2 +0 ---------------------------------------------------------------------- +-------- No. of IPv4 Sessions: 1 ====================================================================== +========
Update: Sorry I missed understood, see bellow another possible solution:
#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $output1 = "======================================================= +======================= LDP Sessions ====================================================================== +======== Peer LDP Id Adj Type State Msg Sent Msg Recv Up Tim +e ---------------------------------------------------------------------- +-------- ---------------------------------------------------------------------- +-------- No Matching Entries Found ====================================================================== +========"; my $output2 = "======================================================= +======================= LDP IPv4 Sessions ====================================================================== +======== Peer LDP Id Adj Type State Msg Sent Msg Recv Up Tim +e ---------------------------------------------------------------------- +-------- 1.1.1.1:0 Targeted Established 822443 822431 273d 15:02:2 +0 ---------------------------------------------------------------------- +-------- No. of IPv4 Sessions: 1 ====================================================================== +========"; say 'Found 1: ' . $output1 if $output1 =~ (/[0-9]+\.[0-9]+\.[0-9]+\.[0 +-9]+/); say 'Found 2: ' . $output2 if $output2 =~ (/[0-9]+\.[0-9]+\.[0-9]+\.[0 +-9]+/); __END__ $ perl test.pl Found 2: ============================================================= +================= LDP IPv4 Sessions ====================================================================== +======== Peer LDP Id Adj Type State Msg Sent Msg Recv Up Tim +e ---------------------------------------------------------------------- +-------- 1.1.1.1:0 Targeted Established 822443 822431 273d 15:02:2 +0 ---------------------------------------------------------------------- +-------- No. of IPv4 Sessions: 1 ====================================================================== +========
Let me know if it works as expected, BR.
|
|---|