This is a test version of Biostars. For the public version, visit https://www.biostars.org.
i have a script in perl but i dont know why i did wrong

my file is

Sequence Aspect
Lbr|31_V2.3640 P
Lbr|31_V2.3640 P
Lbr|31_V2.3640 F
Lbr|31_V2.3640 F
Lbr|31_V2.3640 F
Lbr|31_V2.3640 C
Lbr|31_V2.3640 C
Lbr|31_V2.3640 C
Lbr|20_V2.3410 P
Lbr|20_V2.3410 P
Lbr|20_V2.3410 F
Lbr|20_V2.3410 F
Lbr|20_V2.3410 C
Lbr|20_V2.3410 C
Lbr|20_V2.3340 P
Lbr|20_V2.3340 P
Lbr|21_V2.2020 P
Lbr|21_V2.2020 F
Lbr|21_V2.2020 C
Lbr|21_V2.2020 C
Lbr|21_V2.2020 C
Lbr|32_V2.1550 P
Lbr|32_V2.1550 P
Lbr|32_V2.1550 F
Lbr|32_V2.1550 F
Lbr|25_V2.0180 P
Lbr|25_V2.0180 P
Lbr|25_V2.0180 P
Lbr|25_V2.0180 P
Lbr|25_V2.0180 P
Lbr|25_V2.0180 F
Lbr|25_V2.0180 F
Lbr|25_V2.0180 F
Lbr|25_V2.0180 F
Lbr|25_V2.0180 C
Lbr|29_V2.0590 P
and a want to take only seq and her combinations ex if that seq exist only ( C,P or F) send to file (C, P or F)

if are two send to ( CP ,CF, FP ) send to files corresponding and ifa the seq are three (CPF)send to a one file.

why i did wrong?

#!/usr/bin/perl
use strict;
use warnings;

my @codigo;

my @letra;

my %dados;

open(FILE,"/data/users/msilva/Lbr_Sprot_argot_Result/outpot_de_teste.txt") || die; #abrir o arquiovo

while (<FILE>) {

@codigo = split("\t",$_); #splitar o conteudo # "\t" tab->separacao

@letra = split("\t",$_);

$dados{$codigo[0]}{$letra[1]}++; #pegar o primeiro indice do hash -> tem que dar um valor

#print $codigo[0] . "\n";
}
close(FILE);

foreach my $codigo (keys %dados) {
     #print "$codigo\n";
        foreach my $letra (keys %{$dados{$codigo}}) {
            #print "$letra  $dados{$codigo}{$letra}\n";

        if ($dados{$codigo}{$letra} = 'P') {

                 print "$codigo\n"
                 }
}

}
sequence perl

2 answers

A one-liner:

sort -t ' ' -k1,1 -k2,2 input.txt   | uniq | awk 'BEGIN{prev="";f="";} {if(prev!=$1 && prev!="") {print(prev) >> f;f="";} f=sprintf("%s%s",f,$2); prev=$1;} END { print(prev) >> f;}'

Result:

$ for F in FP CFP P ; do echo "#$F" && cat "$F" ; done
#FP
Lbr|32_V2.1550
#CFP
Lbr|20_V2.3410
Lbr|21_V2.2020
Lbr|25_V2.0180
Lbr|31_V2.3640
#P
Lbr|20_V2.3340
Lbr|29_V2.0590

What is the best resource to learn advanced AWK ??

Give a man a one-liner and he will do bioinformatics for a day.

Teach a man to correct his code, and he will do bioinformatics for life.

Your output is empty, and you don't have any code handling output to different files. There is a lot missing in your script to achieve your goal, but this is the worst mistake:

if ($dados{$codigo}{$letra} = 'P') { ## assigns 'P'

should be:

if ($dados{$codigo}{$letra} eq 'P') { ## checks for string equality

Further information for OP:

The key difference is (x = y) assigns one value to the other.

The phrase (x == y) is checking whether values are equal to each other. 'eq' is the same as '==' but used when comparing strings rather than numbers.

Log in to answer this question.