如何在Linux / Unix中找到与标识符匹配的字符序列?

时间:2018-10-29 15:26:53

标签: linux bash unix sed grep

我有一个名为mytext.fasta的fasta文件。

mytext.fasta

>lcl|NW_001820834.1_gene_4 [locus_tag=SS1G_01081] [db_xref=GeneID:5493597] [partial=5',3'] [location=complement(<6452..>8801)] [gbkey=Gene]
ATGCAATTGGCAGCAGTCCTAAGCCTCGTGGGCTTGGTTACGGCTCAATGTCCGTACGGATTTGACACAC
CACTTCAAAAGCGTGAATCTATTGATGCTCAAGCCAGTAGTTCTAGTTTCTTGAATCAATTCACAATTAA
CGATACCGATGCACACTTTACCACCGACGCAGGTGGGCCTATGCAAGAGGACACTAGTTTGAAAGCTGGG
>lcl|NW_001820834.1_gene_5 [locus_tag=SS1G_01082] [db_xref=GeneID:5493601] [partial=5',3'] [location=<9695..>10785] [gbkey=Gene]
ATGTTTTCCGGTCCCCAGAAACTTGGCAACGCCAAACAAAAATCAATTGGCCTCGCTTGTCACACAATTA
GTCCCCACGAAGCCTTGTACAAACTAGCCACTGGCTCGTCCCGGACCATTAGGGCAATGTTCAACAGAGA
>lcl|NW_001820834.1_gene_6 [locus_tag=SS1G_01083] [db_xref=GeneID:5494096] [partial=5',3'] [location=<12203..>15199] [gbkey=Gene]
ATGAGAGGCAAGCTTGGTGTCACAGTTGCTGCATTTGCGACGGCATTTCTAAATACGACACTTGCTCAAG
ACTCAACATCATCACAAGCGGATGCGGATACTACCACAAGTTATTGTCCCGTTTACACGCTCACAGCTTC
AGTTGATGCCAGCGCACCTATTATCCCAAACATCCACGATCCGCAGGCAATTAATCCACAAGATGTTTGT
CCGGGGTATACTGCATCCAATGTGAAGCGAACCTCTCACGGATTGACGGCTTCTCTGTCATTGGCTGGTG

当我做grep -A1 'SS1G_01082' mytext.fasta时,我得到:

>lcl|NW_001820834.1_gene_5 [locus_tag=SS1G_01082] [db_xref=GeneID:5493601] [partial=5',3'] [location=<9695..>10785] [gbkey=Gene]
ATGTTTTCCGGTCCCCAGAAACTTGGCAACGCCAAACAAAAATCAATTGGCCTCGCTTGTCACACAATTA

我想得到:

>lcl|NW_001820834.1_gene_5 [locus_tag=SS1G_01082] [db_xref=GeneID:5493601] [partial=5',3'] [location=<9695..>10785] [gbkey=Gene]
ATGTTTTCCGGTCCCCAGAAACTTGGCAACGCCAAACAAAAATCAATTGGCCTCGCTTGTCACACAATTA
GTCCCCACGAAGCCTTGTACAAACTAGCCACTGGCTCGTCCCGGACCATTAGGGCAATGTTCAACAGAGA

如果您注意到,此文件中的每个序列都以>开头,因此我想在执行grep时获得序列的完整长度。我该怎么做?

1 个答案:

答案 0 :(得分:1)

gnu awk使用自定义RS更容易:

awk -v RS='(^|\n)>' '/SS1G_01082/{print RT $0}' file

>lcl|NW_001820834.1_gene_5 [locus_tag=SS1G_01082] [db_xref=GeneID:5493601] [partial=5',3'] [location=<9695..>10785] [gbkey=Gene]
ATGTTTTCCGGTCCCCAGAAACTTGGCAACGCCAAACAAAAATCAATTGGCCTCGCTTGTCACACAATTA
GTCCCCACGAAGCCTTGTACAAACTAGCCACTGGCTCGTCCCGGACCATTAGGGCAATGTTCAACAGAGA