В следующем сценарии предполагается, что вы хотите напечатать имена столбцов для всех столбцов в каждой строке ввода (после первой строки заголовка ), которые имеют значение 1
.
#!/usr/bin/perl
use strict;
my @titles=();
while(<>) {
if ($. == 1) {
@titles = split; # get column titles
print "#OTUID\tASVs\n"; # print the new output header
next;
};
chomp;
my @F=split; # split the input line into fields, store in array @F
my @ASVs=(); # @ASV array holds the titles for each matching field.
foreach my $asv (1..$#F) {
push @ASVs, $titles[$asv] if ($F[$asv] == 1);
};
print "$F[0]\t", join(",", @ASVs), "\n";
}
сохраните его как, например, alex.pl
, сделайте его исполняемым с помощью chmod +x alex.pl
и запустите так:
$./alex.pl input.txt
#OTUID ASVs
OTU1 ASV_1,ASV_4,ASV_10
OTU2 ASV_2,ASV_5
OTU3 ASV_6,ASV_8,ASV_9