I want to sort a file content in a specific order that is followed in another file.For example,
Input File1:
dff_0_1:G11
dff_0_5:N_25
dff_0:G10
dff_0_3:G13
dff_0_2:G12
Input File2:
G13
G11
G12
G10
N_25
output File1:
dff_0_3:G13
dff_0_1:G11
dff_0_2:G12
dff_0:G10
dff_0_5:N_25
Here is the perl code i wrote, but its not working as i wish.
my @input_file2 = <IN2>;
chomp @input_File2;
while (<input_file1>) {
foreach my $i (0..$#input_File2){
print OUT43 if /(.+)\:\Q$input_File1[$i]\E/;
}
}
close (IN33);
close (IN43);
close (OUT43);
Use grep
for to do it. grep
returns the matched element from an array, then construct the list for to store output of the grep. use select
to write content of the print statement to a new file.
When using lexical file handler there is no need to close the file
open my $fh1, "<", "one.txt";
open my $fh2 , "<","two.txt";
open my $wh, ">","output.txt";
my @data1 = <$fh1>;
select ($wh);
while (my $line = <$fh2>)
{
chomp $line;
my ($sort_data) = grep{/$line$/} @data1;
print "$sort_data";
}