0

将列从 csv 文件转换为数组格式的最简单方法是什么?

我将有 3 列,例如 la_liga、playernames_laliga 和 teamid_laliga。基本上,我想要任何接近数组格式的东西,有没有办法使用命令行来做到这一点?手动执行此操作的行太多,因此我确实需要可以直接用于进一步操作的基本数组格式。

Athletic Bilbao 336 The Parmtree
Atletico Madrid 798466  Santiagounreal
Barcelona   90402   TW
Celta Vigo  24214   Bonz
Deportivo La Coruna 142758  Boom Shakalaka
Espanyol    1658    Tangtastic
Getafe  8333    Declan McCarthy
Granada 2253    Green Windmill
Levante 14069   In Tommy We Trust
Malaga  4877    Ajay
Mallorca    77201   Emperikal
Osasuna 2714    Declan Quinn
Rayo Vallecano  171 Hillbilly Pete
Real Betis  32696   Billy Gilmore
Real Madrid 4   Demi
Real Sociedad   485 Yossi
Real Zaragoza   227 Scroobius Mac
Sevilla 8157    Chris_White
Valencia    4835    Old School
Valladolid  69246   Pepin

在 CSV 中

"Athletic Bilbao","336","The Parmtree"
"Atletico Madrid","798466","Santiagounreal"
"Barcelona","90402","TW"
"Celta Vigo","24214","Bonz"
"Deportivo La Coruna","142758","Boom Shakalaka"
"Espanyol","1658","Tangtastic"
"Getafe","8333","Declan McCarthy"
"Granada","2253","Green Windmill"
"Levante","14069","In Tommy We Trust"
"Malaga","4877","Ajay"
"Mallorca","77201","Emperikal"
"Osasuna","2714","Declan Quinn"
"Rayo Vallecano","171","Hillbilly Pete"
"Real Betis","32696","Billy Gilmore"
"Real Madrid","4","Demi"
"Real Sociedad","485","Yossi"
"Real Zaragoza","227","Scroobius Mac"
"Sevilla","8157","Chris_White"
"Valencia","4835","Old School"
"Valladolid","69246","Pepin"

期望的输出:

$col1=array("Athletic Bilbao","Atletico Madrid","Barcelona","Celta Vigo","Deportivo La Coruna","Espanyol","Getafe","Granada","Levante","Malaga","Mallorca","Osasuna","Rayo Vallecano","Real Betis","Real Madrid","Real Sociedad","Real Zaragoza","Sevilla","Valencia","Valladolid");
$col2=array('336','798466','90402','24214','142758','1658','8333','2253','14069','4877','77201','2714','171','32696','4','485','227','8157','4835','69246');
$col3=array("The Parmtree","Santiagounreal","TW","Bonz","Boom Shakalaka","Tangtastic","Declan McCarthy","Green Windmill","In Tommy We Trust","Ajay","Emperikal","Declan Quinn","Hillbilly Pete","Billy Gilmore", "Demi","Yossi","Scroobius Mac","Chris_White","Old School","Pepin");

必须有人为此准备好脚本,对吗?

4

1 回答 1

4
$ awk -F, '{for (i=1;i<=NF;i++) col[i]=col[i] (NR==1?"":FS) $i} END{gsub(/"/,"\047",col[2]); for (i=1;i<=NF;i++) printf "$col%d=array(%s);\n",i,col[i]}' file
$col1=array("Athletic Bilbao","Atletico Madrid","Barcelona","Celta Vigo","Deportivo La Coruna","Espanyol","Getafe","Granada","Levante","Malaga","Mallorca","Osasuna","Rayo Vallecano","Real Betis","Real Madrid","Real Sociedad","Real Zaragoza","Sevilla","Valencia","Valladolid");
$col2=array('336','798466','90402','24214','142758','1658','8333','2253','14069','4877','77201','2714','171','32696','4','485','227','8157','4835','69246');
$col3=array("The Parmtree","Santiagounreal","TW","Bonz","Boom Shakalaka","Tangtastic","Declan McCarthy","Green Windmill","In Tommy We Trust","Ajay","Emperikal","Declan Quinn","Hillbilly Pete","Billy Gilmore","Demi","Yossi","Scroobius Mac","Chris_White","Old School","Pepin");
于 2013-08-16T12:15:04.413 回答