#!/usr/bin/perl use 5.010; use strict; use warnings; my $port_name = 'sdm_yout_i[1]'; $port_name =~ s/\[|\]/\\$&/g; # escaping [ and ] open my $fh, "tt.txt" or die $!; while (<$fh>) { if ( /\.([0-9a-z_]+?) \( \{.*?${port_name} .*?,/ ) { print '$port_name = ' ; say $port_name; print '$cell name = ' ; say $1; } } #### $port_name = sdm_yout_i\[1\] $cell name = sdm_yout_i