我试图从这个文件中获取:
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
这看起来像是您有问题的输出。
解决方案是运行dos2unix
或其他实用程序来删除 DOS/Windows 行结尾。
答案2
可能重复(因为我有时也会做同样的事情)
sed ':a;$!N;s/\(\(.*|\)[^|]\+\)\n\2/\1, /;ta;P;D' SEARCH_ECISTORE_PRD_MULTI_ES_s.csv
将产生
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, 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.
答案3
与GNU数据混合:
datamash -t\| -g 1,2 collapse 3 <SEARCH_ECISTORE_PRD_MULTI_ES_s.csv
结果:
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,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.