#!/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 #### ============================================================================== LDP Sessions ============================================================================== Peer LDP Id Adj Type State Msg Sent Msg Recv Up Time ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ No Matching Entries Found ============================================================================== ============================================================================== LDP IPv4 Sessions ============================================================================== Peer LDP Id Adj Type State Msg Sent Msg Recv Up Time ------------------------------------------------------------------------------ 1.1.1.1:0 Targeted Established 822443 822431 273d 15:02:20 ------------------------------------------------------------------------------ No. of IPv4 Sessions: 1 ============================================================================== #### #!/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 Time ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ No Matching Entries Found =============================================================================="; my $output2 = "============================================================================== LDP IPv4 Sessions ============================================================================== Peer LDP Id Adj Type State Msg Sent Msg Recv Up Time ------------------------------------------------------------------------------ 1.1.1.1:0 Targeted Established 822443 822431 273d 15:02:20 ------------------------------------------------------------------------------ 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 Time ------------------------------------------------------------------------------ 1.1.1.1:0 Targeted Established 822443 822431 273d 15:02:20 ------------------------------------------------------------------------------ No. of IPv4 Sessions: 1 ==============================================================================