両方のファイルからデータを抽出したいと思います。
ファイル1.txt:
Type Number ID Element Email
Zed 21-2 9 Blade
ファイル2.txt:
Name Order Email_Address
Ken 19 [email protected]
Tom 21 [email protected]
Ray 23 [email protected]
2つのファイルを1つのファイルにマージする方法は次のとおりです。
Type Number ID Element Email
Zed 21-2 9 Blade [email protected]
以前に試したことは次のとおりです。
awk 'NR==1{print $0;next} NR==FNR{a[$2]=$1"\t"$3"\t"$4";next} {if($2 in a){print(a[$2]"\t"$3)}}' file1.txt file2.txt
if文に問題があり、結果が得られないようです。私が望む結果をどのように取得できますか?
ベストアンサー1
$ awk 'BEGIN{print "Type Number ID Element Email"}NR==FNR{Arr[$2]=$NF;next}{split($2,b,"-");if(b[1] in Arr){print $0,Arr[b[1]]}}' file2.txt file1.txt
Type Number ID Element Email
Zed 21-2 9 Blade [email protected]
Arr[$2]=$NF
-> 2番目の列インデックスを持つ配列に電子メールアドレスを保存します。
split($2,b,"-")
--> 2番目の列の値を分割して照会に使用します。