特定の文字列を含む連続行を見つけ、テーブルに基づいてファイルを変更する

特定の文字列を含む連続行を見つけ、テーブルに基づいてファイルを変更する

解決できないテキスト操作の問題があります。以下のテキストファイル(text.txt)があるとしましょう。場合によっては、行が続く行の/locus_tag後に行が続き、/gene他の場合はそうではありません。/locus_tag後ろにaがないすべての行を見つけ、/gene次の表(table.txt)を使用して/locus_tagaを一致させ、.txtの後のテキストファイルに/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とは異なり、データファイルのテキストの前のスペースで中断されないようにしてくださいFSold=$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。上記で修正しました。

おすすめ記事