Bashの値に基づいて2つのファイルの行を連結します。

Bashの値に基づいて2つのファイルの行を連結します。

Bashスクリプトを使用して2つのファイルを結合しようとしています。

ファイル1:

John (20)
Jim (30)
Adrian Lors (23)

ファイル2:

Jim
some jim info here
some jim other info
more jim info

John
some john info here
some john other info
more john info

Adrian Lors
some adrian info here
some adrian other info
more adrian info

予想される結果は次のようになりますが、試すことはできません。

ファイル3:

Jim (30)
some jim info here
some jim other info
more jim info

Adrian Lors (23)
some adrian info here
some adrian other info
more adrian info

John (20) 

some john info here
some john other info
more john info

理想的には、file3を括弧内の数字でソートしたいのですが、最大の問題は、2つのファイルを結合できないことです。シェルスクリプトでこれをどのように達成できますか?

ありがとう

ベストアンサー1

これがあなたが探しているものかもしれません:

$ cat tst.awk
NR==FNR {
    val = $NF
    gsub(/^[[:space:]]+|[[:space:]]+[^[:space:]]+[[:space:]]*$/,"")
    map[$0] = val;
    next
}
FNR==1 {
    FS = OFS = "\n"
    ORS = "\n\n"
    $0 = $0
}
{
    $1 = $1 " " map[$1]
    print
}

$ awk -f tst.awk file1 RS= file2
Jim (30)
some jim info here
some jim other info
more jim info

John (20)
some john info here
some john other info
more john info

Adrian Lors (23)
some adrian info here
some adrian other info
more adrian info

おすすめ記事