#! /usr/bin/perl use strict; use warnings; my $A = 2; my $file = '15ug.txt'; open(my $fh,"<$file") or die $!; while(<$fh>){ print if(/5ug$A/.../6\*End/); } close $fh;