
해결할 수 없는 텍스트 조작 문제가 있습니다. 아래와 같은 텍스트 파일(text.txt)이 있다고 가정해 보겠습니다. /locus_tag
with 줄 다음에 with 줄이 오는 경우 /gene
와 그렇지 않은 줄이 있는 경우가 있습니다 . 뒤에 /locus_tag
오지 않는 모든 줄을 찾은 /gene
다음 아래와 같은 테이블(table.txt)을 사용하여 를 /locus_tag
a와 일치 /gene
시키고 ./gene
/locus_tag
이 작업을 수행하는 방법에 대한 아이디어가 있으면 좋을 것입니다.
/locus_tag="LOCUS_23770"
/note="ABC"
/locus_tag="LOCUS_23780"
/note="DEF"
/locus_tag="LOCUS_23980"
/note="GHI"
/locus_tag="LOCUS_24780"
/gene="BT_4758"
/note="ONP"
/locus_tag="LOCUS_25780"
/gene="BT_4768"
/note="WZX"
테이블
/locus_tag /gene
LOCUS_00010 BT_4578
LOCUS_00020 BT_4577
LOCUS_00030 BT_2429
답변1
연결된 파일을 사용하면 작동합니다.
awk 'BEGIN{FS="[ =]+"; OFS="="}
BEGINFILE{fno++}
fno==1{locus["\""$1"\""]="\""$2"\""; }
fno>1{if (old ~ /LOCUS/ && $0 !~ /gene/) print "/gene", locus[old]; old=$3; print}
' table file1
전에
/locus_tag="LOCUS_00030"
/note="WP_011108293.1 hypothetical protein (Bacteroides
후에
/locus_tag="LOCUS_00030"
/gene="BT_2429"
/note="WP_011108293.1 hypothetical protein (Bacteroides
awk
연습 에 익숙하지 않기 때문에
awk 'BEGIN{FS="[ =]+"; OFS="="}
# set up the input field separator as any group of spaces and/or =
# and set the output field separator as =
BEGINFILE{fno++}
# Whenever you open a file, increment the file counter fno
fno==1{locus["\""$1"\""]="\""$2"\""; }
# if this is the first file (i.e. table) load the array `locus[]`
# but wrap the fields in "..." so that they are exactly like the data file entries
fno>1{if (old ~ /LOCUS/ && $0 !~ /gene/) print "/gene", locus[old]; old=$3; print}
# if this is a data file
# if the current value of old (i.e. the previous line) is a LOCUS
# and && this line ($0) isn't a gene
# add a gene by indexing into the locus array based upon the value of old
# because old contains the last LOCUS we found
# in all cases
# set old to the 3rd field on the current line,
# which on any LOCUS line is the string "LOCUS_?????" and
# print the current line
# See note below re $2 vs $3 and FS
' table file1
# your input files, table must be first, you can have more data files if you want
또는 multichar가 없으면 multichar 가 수행하는 데이터 파일의 텍스트 앞 공백에서 중단되지 않기 때문에 FS
유지합니다 .old=$2
아래에서는 읽고 있는 파일에 따라 필드 구분 기호를 설정합니다 FS=(fno==1)?" ":"="
. 테이블과 =
데이터를 위한 공간
awk 'BEGIN{OFS="="}
BEGINFILE{fno++;FS=(fno==1)?" ":"="}
fno==1{locus["\""$1"\""]="\""$2"\""; }
fno>1{if (old ~ /LOCUS/ && $0 !~ /gene/) print "/gene", locus[old]; old=$2; print}
' table file1
테이블 파일이 메모리를 먹을 만큼 크지 않다면 말이죠.
그리고 비어 있는 것 이상으로 적합한 경우 누락된 유전자에 메시지를 삽입하는 테스트를 실시합니다./gene=
fno>1{if (old ~ /LOCUS/ && $0 !~ /gene/) print "/gene", (old in locus)?locus[old]:"\"GENE_MISSING_AT_LOCUS\""; old=$3; print}
사용 중인 old
버전과 일치하도록 필드 참조를 변경하세요.FS
/locus_tag="LOCUS_00020"
/gene="GENE_MISSING_AT_LOCUS"
/note="WP_008765457.1 hypothetical protein (Bacteroides
편집하다
연결한 샘플 파일을 보면 위의 샘플과 필드 번호가 엉망인 실제 데이터 간의 형식 차이에 문제가 있는 것뿐입니다. old=$2
그냥 으로 변경하면 됩니다 old=$3
. 위에서 수정했습니다.