awk、sed、grepを使用して複数のファイルの内容を追加する

awk、sed、grepを使用して複数のファイルの内容を追加する

ファイル1とファイル2の名前を次の形式で指定する必要があります。

ファイル1には18000行があります。

COL1 COL2 COL3 COL4 COL5

-   .   ID  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   2   Parent  =   tom_fa_10005086
-   1   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
+   .   ID  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
-   .   ID  =   tom_fa_10000024
-   0   Parent  =   tom_fa_10000024
-   .   ID  =   tom_fa_10004587
-   0   Parent  =   tom_fa_10004587
-   1   Parent  =   tom_fa_10004587
-   1   Parent  =   tom_fa_10004587
+   .   ID  =   tom_fa_10018753
+   0   Parent  =   tom_fa_10018753
+   2   Parent  =   tom_fa_10018753
+   2   Parent  =   tom_fa_10018753
+   0   Parent  =   tom_fa_10018753
+   .   ID  =   tom_fa_10007441
+   0   Parent  =   tom_fa_10007441
+   0   Parent  =   tom_fa_10007441
+   1   Parent  =   tom_fa_10007441
+   0   Parent  =   tom_fa_10007441
+   1   Parent  =   tom_fa_10007441
+   2   Parent  =   tom_fa_10007441

そしてファイル2

COL 1            COL 2

tom_fa_10005086 ABI3VP1
tom_fa_10013928 ABI3VP2
tom_fa_10000024 Alfin-like
tom_fa_10011338 C2C2-Dof
tom_fa_10003474 C2C2-Gata
tom_fa_10003291 C2H2
tom_fa_10007064 C2H2
tom_fa_10008972 C2H2
tom_fa_10009025 C2H2
tom_fa_10005726 C3H
tom_fa_10011317 C3H
tom_fa_10010708 CPP
tom_fa_10002138 E2F-DP
tom_fa_10009443 G2-like
tom_fa_10008970 GRAS

私がしたいことは、(ファイル1)の列5と列1(ファイル2)が同じ場合、列2(ファイル2)に対応するファイル1の列6に追加することです。

だからそれは

-   .   ID  =   tom_fa_10005086   ABI3VP1
-   0   Parent  =   tom_fa_10005086   ABI3VP1
-   0   Parent  =   tom_fa_10005086   ABI3VP1
-   2   Parent  =   tom_fa_10005086   ABI3VP1
-   1   Parent  =   tom_fa_10005086   ABI3VP1
-   0   Parent  =   tom_fa_10005086   ABI3VP1
-   0   Parent  =   tom_fa_10005086   ABI3VP1
+   .   ID  =   tom_fa_10013928   ABI3VP2
+   0   Parent  =   tom_fa_10013928   ABI3VP2
+   0   Parent  =   tom_fa_10013928   ABI3VP2
+   0   Parent  =   tom_fa_10013928   ABI3VP2
+   0   Parent  =   tom_fa_10013928   ABI3VP2
+   2   Parent  =   tom_fa_10013928   ABI3VP2
+   2   Parent  =   tom_fa_10013928   ABI3VP2
+   1   Parent  =   tom_fa_10013928   ABI3VP2
+   1   Parent  =   tom_fa_10013928   ABI3VP2
+   2   Parent  =   tom_fa_10013928   ABI3VP2
+   2   Parent  =   tom_fa_10013928   ABI3VP2
-   .   ID  =   tom_fa_10000024   Alfin-like
-   0   Parent  =   tom_fa_10000024   Alfin-like
-   .   ID  =   tom_fa_10004587
-   0   Parent  =   tom_fa_10004587
-   1   Parent  =   tom_fa_10004587
-   1   Parent  =   tom_fa_10004587
+   .   ID  =   tom_fa_10018753
+   0   Parent  =   tom_fa_10018753
+   2   Parent  =   tom_fa_10018753
+   2   Parent  =   tom_fa_10018753
+   0   Parent  =   tom_fa_10018753
+   .   ID  =   tom_fa_10007441
+   0   Parent  =   tom_fa_10007441
+   0   Parent  =   tom_fa_10007441
+   1   Parent  =   tom_fa_10007441
+   0   Parent  =   tom_fa_10007441
+   1   Parent  =   tom_fa_10007441
+   2   Parent  =   tom_fa_10007441

fgrep -f file 2 file1を試しましたが、列2 file2の情報をマージする方法がわからず、文字列が同じではありません。

ベストアンサー1

一度に実行するには、次のsedスクリプトが機能します。

sed '/^[^ ]* [^ ]*$/{H;d;};G;s/ \([^ ]*\)\n.*\n\1 \([[:print:]]*\).*/ \1   \2/;P;d' file2 file1

それは何をしますか?file2予約済みスペースのすべての行を一種のルックアップテーブルとして収集し、そのテーブルをパターンスペースの各行に追加し、パターンが見つかったら6番目の列を追加します。詳細:

  • /^[^ ]* [^ ]*$/{}2つの列を持つ行を一致させるには、その列に対してのみ次のコマンドを実行します(内部コマンド
  • {H;d;}この行を予約済みスペースに追加し、その行の追加処理を停止するため、残りのスクリプトは次の行に対してのみ実行されます。file1
  • Gパターンスペースにルックアップテーブルを含む予約済みスペースを追加します。
  • s/ \([^ ]*\)\n.*\n\1 \([[:print:]]*\).*/ \1 \2/一致に 6 番目の列を追加するかどうか
  • P添付テーブルなしで行を印刷する
  • d他の出力抑制(オプションも-n同じことを行います)

おすすめ記事