#!/usr/bin/perl use strict; use warnings; use Getopt::Long; GetOptions( 'sp=s' => \my $sp, 'p=n' => \my $port, ); my $file = 'file.txt'; open my $fh, '<', $file or die $!; { local $/ = "\n\n"; while(my $block = <$fh> ){ next unless index( $block, 'SP Name: SP ' . $sp ) != -1 and index( $block, 'SP Port ID: ' . $port ) != -1; my ($status) = $block =~ /Link Status: (\w+)/; print "Link status for SP $sp (Port $port): $status\n"; } } close $fh;