文字列を別のファイルの同じ単語に置き換える

文字列を別のファイルの同じ単語に置き換える

ファイルを編集し1.txt、単語を見つけてその単語に置き換えます2.txt2.txt

私のファイルの順序を維持することに興味があります1.txt

>title1
ID1 .... rest of string I'm not interested in
>title2
ID2 .... rest of string I'm not interested in
>title3
ID3 .... rest of string I'm not interested in
>title....

しかし、私の情報を追加したいと思います2.txt

>ID1  text I want to extract
>ID2  text I want to extract
>ID3  text I want to extract
>IDs....

最後に、次の構造の新しいファイルを作成したいと思います。

>title1
ID1 .... text I want
>title2
ID2 .... text I want
>title3
ID3 .... text I want
>title....

私は複数のsedコマンドを試しましたが、ほとんどは両方のファイルのID#を完全に置き換えません。 Bashで処理できることを願っています。

ご協力ありがとうございます

試行に失敗しました。私のコードは、ファイル1 = cog_anotations.txt、ファイル2 = Real.cog.txt ID = COG05764、COG 015668などです。

sed -e '/COG/{r Real.cog.txt' -e 'd}' cog_anotations.txt
sed "s/^.*COG.*$/$(cat Real.cog.txt)/" cog_anotations.txt
sed -e '/\$COG\$/{r Real.cog.txt'  -e 'd}' cog_anotations.txt
grep -F -f cog_anotations.txt Real.cog.txt > newfile.txt
grep -F -f Real.cog.txt cog_anotations.txt > newfile.txt

実線ファイル1

>Bravo_5
>CDD:223731 COG0658, ComEC, Predicted membrane metalbinding protein l 
>Bravo_6
>CDD:223242 COG0164, RnhB, Ribonuclease HII [DNA replication, 
>Bravo_7
>CDD:223778 COG0706, YidC, Preprotein translocase subunit YidC .

実線ファイル2

COG0006    E    Xaa-Pro aminopeptidase
COG0706    J    Glutamyl- or glutaminyl-tRNA synthetase
COG0164    J    tRNA A37 threonylcarbamoyladenosine synthetase subunit 
COG0012    J    Ribosome-binding ATPase YchF, GTP1/OBG family
COG0013    J    Alanyl-tRNA synthetase

ベストアンサー1

そしてawk

awk 'NR==FNR{id[$1","]=$0}
  NR!=FNR{f=$0; getline; if (id[$2]) print f RS id[$2]}' file2 file1

>Bravo_6
COG0164    J    tRNA A37 threonylcarbamoyladenosine synthetase subunit 
>Bravo_7
COG0706    J    Glutamyl- or glutaminyl-tRNA synthetase

id目的のテキストで配列をロードし、他のファイルと一致するようにfile2追加,

awk 'NR==FNR{id[$1","]=$0}

2番目のファイルのNR!=FNR最初の行をつかみ、f2番目のファイルに移動します。getline

  NR!=FNR{f=$0; getline; 

id次に、目的の配列にあることをテストし、(id[$2])存在する場合は印刷します。

  if (id[$2]) print f RS id[$2]}' file2 file1

おすすめ記事