パターン検索や他のファイルに行を追加する

パターン検索や他のファイルに行を追加する

次のファイルがあります(5つのタブで区切られた列)。

head allKO.txt
Metabolism Carbohydrate metabolism Glycolisis K07448
Metabolism Protein metabolism protesome K02217

ファイルの5番目の列でパターン(文字列)を検索し、見つかったらパターンが見つかった行とすべての列をKEGG.annotations別のファイルに印刷したいと思います。パターンを探しているファイルは次のとおりです。KEGG.annotationsallKO.txt

head KEGG.annotations
>aai:AARI_24510  proP; proline/betaine transporter; K03762 MFS transporter, MHS family, proline/betaine transporter
>aai:AARI_26600  ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
>aai:AARI_28260  hypothetical protein
>aai:AARI_29060  ABC drug resistance transporter, inner membrane subunit; K09686 antibiotic transport system permease protein
>aai:AARI_29070  ABC drug resistance transporter, ATP-binding subunit (EC:3.6.3.-); K09687 antibiotic transport system ATP-binding protein
>aai:AARI_29650  hypothetical protein
>aai:AARI_32480  iron-siderophore ABC transporter ATP-binding subunit (EC:3.6.3.-); K02013 iron complex transport system ATP-binding protein [EC:3.6.3.34]
>aai:AARI_33320  mrr; restriction system protein Mrr; K07448 restriction system protein

私は次のようなものが欲しい:

Metabolism Carbohydrate metabolism Glycolisis K07448 >aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system
Metabolism Protein metabolism proteasome K02217  >aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]

最初の行に追加された>aai:AARI_33320 mrr; restriction …テキストは8行目で、これには最初の行(5番目のフィールド)のIDフィールドがKEGG.annotations含まれます。K07448allKO.txt

私のスキーマファイルを使用するには、このコードをどのように変更する必要がありますか?これは、探している特定のパターンを含む列が1つしかないパターンファイルに適用されます。

while read pat; do
    grep "$pat" --label="$pat" -H < KEGG.annotations;
done < allKO.txt > test1

ベストアンサー1

既存のコードを使用できます。行を配列に保存し、5番目の要素と一致させます。

while read -r line; do
    [ -z "$line" ] && continue
    patlist=($line)
    pat=${patlist[4]}
    grep "$pat" --label="$line" -H < KEGG.annotations
done < allKO.txt

返品:

Metabolism Carbohydrate metabolism Glycolisis K07448:>aai:AARI_33320  mrr; restriction system protein Mrr; K07448 restriction system protein
Metabolism Protein metabolism protesome K02217:>aai:AARI_26600  ferritin-like protein; K02217 ferritin [EC:1.16.3.1]

おすすめ記事