Eu tenho este arquivo de 5 colunas:
m64071_220512_054244/46858502/ccs TCTACACGACGCTCTTCCGATCTTATTGGGCACGGTGTCGCCATCTGATCGTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTCGAGGTTTGCAGCTATTTTATTTACAAGTATACATTTAACACAATGAAATAAACACTGATATACTGAAGCCTAGTTAATAGTAGTGTAACAATATGCATCATTTTGATGATTACATTATTTTAAACAACAAACTACACTGAAAAATTAATGCCGATAAAATTCTTGGTCATAATATTAAGAAATACAATATATAAATTGAAAATATGATTGCTTAAAATTTGAAAATGGAAGTGAACTCATTTGGACAGACTCAGAGTTAACATAATCTGAAGGGAGGGGAGCTCTGACCCAAATGATATCTTTCAGGTTAACAGAAGAAAAAAGAAGCATAGTTTATCTTCAAGGAGAACGGGCAGTTTGCTTCTTCAGGTA fwd pet047-9952 TATTGGGCACGGTGTC
m64071_220512_054244/52233509/ccs AGCTTTTTTGGAATCTTCTGCTAAAGAAAATCAGACTGCTGTGGATGTTTTTCGAAGGATAATTTTGGAGGCAGAAAAAATGGACGGGGCAGCTTCACAAGGCAAGTCTTCATGCTCGGTGATGTGATTCTGCTGCAAAGCCTGAGGACACTGGGAATATATTCTACCTGAAGAAGCAAACTGCCCGTTCTCCTTGAAGATAAACTATGCTTCTTTTTTCTTCTGTTAACCTGAAAGATATCATTTGGGTCAGAGCTCCCCTCCCTTCAGATTATGTTAACTCTGAGTCTGTCCAAATGAGTTCACTTCCATTTTCAAATTTTAAGCAATCATATTTTCAATTTATATATTGTATTTCTTAATATTATGACCAAGAATTTTATCGGCATTAATTTTTCAGTGTAGTTTGTTGTTTAAAATAATGTAATCATCAAAATGATGCATATTGTTACACTACTATTAACTAGGCTTCAGTATATCAGTGTTTATTTCATTGTGTTAAATGTATACTTGTAAATAAAATAGCTGCAAACCTCGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACGATCAGATGGCGACACCGTGCCCAATAAGATCGGAAGAGCGTCGTGTAGA rev pet047-9952 GACACCGTGCCCAATA
m64071_220512_054244/91226755/ccs TCTACACGACGCTCTTCCGATCTTATTGGGCACGGTGTCGCCATCTGATCGTTTTTTTTTTTTTTTTTTTTTTTTTTTTTCGAGGTTTGCAGCTATTTTATTTACAAGTATACATTTAACACAATGAAATAAACACTGATATACTGAAGCCTAGTTAATAGTAGTGTAACAATATGCATCATTTTGATGATTACATTATTTTAAACAACAAACTACACTGAAAAATTAATGCCGATAAAATTCTTGGTCATAATATTAAGAAATACAATATATAAATTGAAAATATGATTGCTTAAAATTTGAAAATGGAAGTGAACTCATTTGGACAGACTCAGAGTTAACATAATCTGAAGGGAGGGGAGCTCTGACCCAAATGATATCTTTCAGGTTAACAGAAGAAAAAAGAAGCATAGTTTATCTTCAAGGAGAACGGGCAGTTTGCTTCTTCAGGTAGAATATATTCCCAGTGTCCTCAGGCTTTGCAGCAGAATCACATCACCGAGCATGAAGACTTGCCTTGTGAAGCTGCCCCGTCCATTTTTTCTGCCTCCAA fwd pet047-9952 TATTGGGCACGGTGTC
Para cada linha, preciso usar o grep do valor da última coluna $5 no segundo campo $2. Então, preciso imprimir a mesma linha com uma coluna $6 adicional com o resultado do grep com uma condição:, if ($3 == rev)
o $6 é o resultado do grep + 12 caracteres após o grep ou if ($3 == fwd)
o resultado do grep + 12 caracteres antes do grep.
awk '$2~/$5/ {match($0, /$5/); if ($4=="rev") print substr($0, RSTART +12, RLENGTH + 12); else print substr($0, RSTART + 0, RLENGTH + 12) ;}' file
Os valores de $ 5 são necessários 16 caracteres e o padrão que procuro é sempre de 12 caracteres. Então, minha saída de $ 6 tem 28 caracteres.
Resultado esperado:
m64071_220512_054244/46858502/ccs TCTACACGACGCTCTTCCGATCTTATTGGGCACGGTGTCGCCATCTGATCGTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTCGAGGTTTGCAGCTATTTTATTTACAAGTATACATTTAACACAATGAAATAAACACTGATATACTGAAGCCTAGTTAATAGTAGTGTAACAATATGCATCATTTTGATGATTACATTATTTTAAACAACAAACTACACTGAAAAATTAATGCCGATAAAATTCTTGGTCATAATATTAAGAAATACAATATATAAATTGAAAATATGATTGCTTAAAATTTGAAAATGGAAGTGAACTCATTTGGACAGACTCAGAGTTAACATAATCTGAAGGGAGGGGAGCTCTGACCCAAATGATATCTTTCAGGTTAACAGAAGAAAAAAGAAGCATAGTTTATCTTCAAGGAGAACGGGCAGTTTGCTTCTTCAGGTA fwd pet047-9952 TATTGGGCACGGTGTC TATTGGGCACGGTGTCGCCATCTGATCG
m64071_220512_054244/52233509/ccs AGCTTTTTTGGAATCTTCTGCTAAAGAAAATCAGACTGCTGTGGATGTTTTTCGAAGGATAATTTTGGAGGCAGAAAAAATGGACGGGGCAGCTTCACAAGGCAAGTCTTCATGCTCGGTGATGTGATTCTGCTGCAAAGCCTGAGGACACTGGGAATATATTCTACCTGAAGAAGCAAACTGCCCGTTCTCCTTGAAGATAAACTATGCTTCTTTTTTCTTCTGTTAACCTGAAAGATATCATTTGGGTCAGAGCTCCCCTCCCTTCAGATTATGTTAACTCTGAGTCTGTCCAAATGAGTTCACTTCCATTTTCAAATTTTAAGCAATCATATTTTCAATTTATATATTGTATTTCTTAATATTATGACCAAGAATTTTATCGGCATTAATTTTTCAGTGTAGTTTGTTGTTTAAAATAATGTAATCATCAAAATGATGCATATTGTTACACTACTATTAACTAGGCTTCAGTATATCAGTGTTTATTTCATTGTGTTAAATGTATACTTGTAAATAAAATAGCTGCAAACCTCGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACGATCAGATGGCGACACCGTGCCCAATAAGATCGGAAGAGCGTCGTGTAGA rev pet047-9952 GACACCGTGCCCAATA CGATCAGATGGCGACACCGTGCCCAATA
m64071_220512_054244/91226755/ccs TCTACACGACGCTCTTCCGATCTTATTGGGCACGGTGTCGCCATCTGATCGTTTTTTTTTTTTTTTTTTTTTTTTTTTTTCGAGGTTTGCAGCTATTTTATTTACAAGTATACATTTAACACAATGAAATAAACACTGATATACTGAAGCCTAGTTAATAGTAGTGTAACAATATGCATCATTTTGATGATTACATTATTTTAAACAACAAACTACACTGAAAAATTAATGCCGATAAAATTCTTGGTCATAATATTAAGAAATACAATATATAAATTGAAAATATGATTGCTTAAAATTTGAAAATGGAAGTGAACTCATTTGGACAGACTCAGAGTTAACATAATCTGAAGGGAGGGGAGCTCTGACCCAAATGATATCTTTCAGGTTAACAGAAGAAAAAAGAAGCATAGTTTATCTTCAAGGAGAACGGGCAGTTTGCTTCTTCAGGTAGAATATATTCCCAGTGTCCTCAGGCTTTGCAGCAGAATCACATCACCGAGCATGAAGACTTGCCTTGTGAAGCTGCCCCGTCCATTTTTTCTGCCTCCAA fwd pet047-9952 TATTGGGCACGGTGTC TATTGGGCACGGTGTCGCCATCTGATCG
Mas não consigo o que quero.
Você pode usar o script abaixo para alcançar seu resultado:
Basicamente, o roteiro:
primeiro encontra o índice do
$5
valor no segundo campo. Se o valor for encontrado(idx != 0)
.a. ele verifica o valor do terceiro campo.
b. Se for
"rev"
, extrai a28-character
substring começando 12 caracteres antes do índice.c. caso contrário, extrai a substring começando no índice.
CÓDIGO DE DEMONSTRAÇÃO
Algo assim fará isso, usando qualquer awk:
Para sua informação, para perguntas futuras, aqui está como seria um exemplo reproduzível mínimo
2
, usando em vez de12
o número de caracteres adicionais a serem impressos:Exemplo de entrada:
Resultado esperado:
com o qual poderíamos testar para ver facilmente se a solução potencial funciona ou não: