Skip to content
Advertisement

Organize file by Linux

I want to thank the great help and support. Well, I have the following file:

name   id alello chr    pos snp    
s1     215     a    1   11  a215    
s1     216     a    1   11  a216    
s1     217     a    1   11  a217    
s1     218     a    1   11  a218    
s2     215     b    2   22  b215    
s2     216     b    2   22  b216    
s2     217     b    2   22  b217    
s2     218     b    2   22  b218

and I need that file be this way:

snp allelo  chr pos 215     216      217    218    
s1     a    1   11  a215    a216    a217    a218    
s2     b    2   22  b215    b216    b217    b218

Please, someone can help me? Thank you very much

Advertisement

Answer

awk to the rescue!

$ awk 'NR==1{h=$1 OFS $3 OFS $4 OFS $5; next} 
            {a[$1]=(($1 in a)?(a[$1] OFS $NF):($3 OFS $4 OFS $5 OFS $6));
             if(!($2 in b)) {h=h OFS $2; b[$2]}} 
        END{print h; for(k in a) print k,a[k]}' file | column -t

name  alello  chr  pos  215   216   217   218
s1    a       1    11   a215  a216  a217  a218
s2    b       2    22   b215  b216  b217  b218
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement