use Modern::Perl; print "\n Enter the file name :>"; chomp( my $filename1 = ); open my $fh, '<', $filename1 or die "Cannot find the file $filename1: $!"; my @line = <$fh>; close $fh; open my $w, '>', 'Result.txt' or die "Cannot open the file Result.txt: $!"; print $w $line[0]; for (@line) { my ( $fourth, $tenth ) = ( split ' ' )[ 4, 10 ]; print $w $_ if $fourth eq 'chr1' and $tenth eq 'chr1'; # did you mean $fourth =~ /chr1/ and $tenth =~ /chr1/ } close $w;