문자열을 다른 파일의 동일한 단어로 대체

문자열을 다른 파일의 동일한 단어로 대체

파일을 편집하고 1.txt, 단어를 찾아서 해당 단어로 바꾸고 2.txt, 2.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}

두 번째 파일의 NR!=FNR첫 번째 줄을 잡고 f두 번째 파일로 이동합니다.getline

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

id그런 다음 원하는 배열에 있는지 테스트 (id[$2])하고 존재하는 경우 인쇄하십시오.

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

관련 정보