divide arquivos com padrão específico

0

Um arquivo com o qual trabalho é como

NAMES   n0  n1  n2  n3  n4  n5  n6  n7
REGION  chr 1   100000
404 AAAAAAGA
992 TTTTTTTA
1146    CCCCGGCC
1727    CCCCCACC
1778    GCCCCCCC

Minha saída desejada é (por favor, observe a linha dois e como os números estão lá)

file1
 NAMES  n0  n1  n2  n3  n4  n5  n6  n7
    REGION  chr 404 992
    404 AAAAAAGA
    992 TTTTTTTA

file2
 NAMES  n0  n1  n2  n3  n4  n5  n6  n7
     REGION chr 1146    1778
1146 CCCCGGCC
1727 CCCCCACC
1778 GCCCCCCC

Eu tentei no awk

awk 'function print_vals() {
   fn="file" c;
   print hdr > fn;
   print "REGION  chr", sn, en >> fn;
   for (i in a)
      print a[i] >> fn;
} NR == 1 {
   hdr=$0;
   c=0;
   next
} NF==2 && $1 >= 1000000*c {
   if (c)
      print_vals();
   delete a;
   i=0;
   c++;
   sn=$1;
} NF==2 {
   a[++i]=$0;
   en=$1;
} END {
   print print_vals();
 }' file

Para os dados de amostra que funcionaram e obtive a saída, mas para o conjunto de dados reais, não. O conjunto está aqui link Ele é parecido com isso

NAMES   n1  n2  n3  n4  n5  n6  n7  n8  n9  n10     n11     n12     n13     n14     n15     n16 $
REGION  chr     1   10000000
69  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
474     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
584     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
627     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
676     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
690     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
894     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
1104    AAAAAAAAAAAAAAAAA

com a saída sendo - que é comutada de alguma forma ... não é assim que deve ser

NAMES   n1  n2  n3  n4  n5  n6  n7  n8  n9  n10     n11     n12     n13     n14     n15     n16 $
REGION  chr 69 999927
561321  AAAAAACAAAAAAAAACAAAAAAAAAAAAAAAAAACCCAAAACAACAAAACAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAACAAAAACCAACA$
561362  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
562011  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
562029  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
562162  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$
562171

alguém poderia me dizer como eu preciso consertar isso ou sugerir outra função?

    
por ktoui 18.07.2015 / 12:12

1 resposta

0

Em matrizes associativas awk são percorridas em uma ordem não determinada. Substitua seu

for (i in a)

por

n = i
for(i=1;i<=n;i++)

Para incluir seu awk em um script bash, você pode usar algo como

#!/bin/bash
for file
do  awk -v file="$file" '....' "$file"
done

Certifique-se de executar chmod a+rx no arquivo de script. Substitua também a linha

fn="file" c;

no seu script awk por

fn = c "_" file;

Esta linha é como o seu novo nome de arquivo é construído: a variável awk file é inicialmente dada como valor o nome do arquivo sendo processado (a sintaxe é: awk -v variable = value). A variável awk fn é sua novo nome de arquivo, e é a variável c que contém o número, concatenado com o caractere _ e variável de nome de arquivo.

Você pode executar este comando bash com vários arquivos como argumentos. Eles são processados pelo awk um por um.

Resultado final:

#!/bin/bash
for file
do awk -v file="$file" 'function print_vals() {
   fn = c "_" file;
   print hdr > fn;
   print "REGION  chr", sn, en >> fn;
   n = i
   for(i=1;i<=n;i++)
      print a[i] >> fn;
 } NR == 1 {
   hdr=$0;
   c=0;
   next
 } NF==2 && $1 >= 1000000*c {
   if (c)
      print_vals();
   delete a;
   i=0;
   c++;
   sn=$1;
 } NF==2 {
   a[++i]=$0;
   en=$1;
 } END {
   print print_vals();
 }'  "$file"
done
    
por meuh 18.07.2015 / 13:58