my @result; my @result1; my @result2; my $b; my $c; my $d; my $ifile51 = '../test.txt'; my $ofile51 = 'seq.txt'; open (my $ifh51, '<', $ifile51) or die $!; open (my $ofh51, '>', $ofile51) or die $!; while(my $line51 = <$ifh51>) { # my $line51 = $_ if /\bbist_mode\b/; @result = grep (/bist_mode/, $line51); @result1 = grep (/mode_sel/, $line51); @result2 = grep (/mem_type_sel/, $line51); $b = join("_",@result); $b =~ s/,([^,]*$)/$1/; $c = join("_",@result1); $c =~ s/,([^,]*$)/$1/; $d = join("_",@result2); $d =~ s/,([^,]*$)/$1/; print $b; print $c; print $d; } print $b; print $c; print $d;