最初の2つの列を結合するフィールド行は次のとおりです。

最初の2つの列を結合するフィールド行は次のとおりです。

このファイルからインポートしようとしています。

A4690021|product.actor|Laurel Cronin
A4690021|product.actor|Bob Hoskins
A4690021|product.actor|Caroline Goodall
A4690021|product.actor|Julia Roberts
A4690021|product.actor|Maggie Smith
A4690021|product.actor|Amber Scott
A4690021|product.actor|Charlie Korsmo
A4690021|product.actor|Robin Williams
A4690021|product.actor|Dustin Hoffman
A4690021|product.director|Steven Spielberg
A4690021|product.bestSellers|Offline Best Seller
A4690021|product.bestSellers|Online Best Seller
A4690021|product.parentCategory.id|999.54215013.999.54216013
A4690021|product.storeName|Cine
A4690021|product.parentCat.displayName|Infantil
A19129625|product.author|. VV.AA.

この結果は次のとおりです。

A4690021|product.actor|Laurel Cronin,Bob Hoskins,Caroline Goodall,Julia Roberts,Maggie Smith,Amber Scott,Charlie Korsmo,Robin Williams,Dustin Hoffman
A4690021|product.director|Steven Spielberg
A4690021|product.bestSellers|Offline Best Seller,
A4690021|product.parentCategory.id|999.54215013.999.54216013
A4690021|product.storeName|Cine
A4690021|product.parentCat.displayName|Infantil
A19129625|product.author|. VV.AA.

このawkスクリプトを試してください:

awk -F'|' '{a[$1"|"$2]=a[$1"|"$2]","$3}END{for(x in a)print x""a[x]}' SEARCH_ECISTORE_PRD_MULTI_ES_s.csv

しかし、なぜこのような結果が出るのかわかりません。

A4690021|product.storeName,Cine
A4690021|product.parentCategory.id,999.54215013.999.54216013
A19129625|product.author,. VV.AA.
A4690021|product.director,Steven Spielberg
,Dustin Hoffmanllactor,Laurel Cronin
A4690021|product.parentCat.displayName,Infantil
,Online Best SellerstSellers,Offline Best Seller

アクターを読むのに問題があります。 $ 3に正しい値が含まれているようですが、集計すると奇妙な方法でマージされます。

ベストアンサー1

最後のフィールドのフィールド区切り文字を変更するには、次の手順を実行します。

$ awk -F'|' '{a[$1"|"$2]=a[$1"|"$2]","$3} END{for(x in a)print x"|"substr(a[x],2)}' file.csv
A4690021|product.storeName|Cine
A4690021|product.parentCategory.id|999.54215013.999.54216013
A19129625|product.author|. VV.AA.
A4690021|product.director|Steven Spielberg
A4690021|product.actor|Laurel Cronin,Bob Hoskins,Caroline Goodall,Julia Roberts,Maggie Smith,Amber Scott,Charlie Korsmo,Robin Williams,Dustin Hoffman
A4690021|product.parentCat.displayName|Infantil
A4690021|product.bestSellers|Offline Best Seller,Online Best Seller

改行の問題

ソースファイルをDOS/Windows形式に変換してみましょう。

$ unix2dos <file.csv >file.dos
$ awk -F'|' '{a[$1"|"$2]=a[$1"|"$2]","$3} END{for(x in a)print x"|"substr(a[x],2)}' file.dos
A4690021|product.storeName|Cine
A4690021|product.parentCategory.id|999.54215013.999.54216013
A19129625|product.author|. VV.AA.
A4690021|product.director|Steven Spielberg
,Dustin Hoffmanllactor|Laurel Cronin
A4690021|product.parentCat.displayName|Infantil
,Online Best SellerstSellers|Offline Best Seller

問題のある出力のようです。

回避策は、dos2unixDOS/Windows 行末を削除する別のユーティリティを実行することです。

おすすめ記事