Pratik Bothra
Pratik Bothra

Reputation: 2694

Convert column data to array for further processing

What is the easiest way to covert a column from a csv file to an array format?

I'd have 3 columns like la_liga, playernames_laliga,, and teamid_laliga. Basically, I'd want anything close to the array format, is there a way to do this using command line? There are way too many rows to do this manually so I do need the basic array format that I can directly use for further manipulation.

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

which in 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"

Desired Output:

$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");

Someone must have a ready-made script for this, right?

Upvotes: 0

Views: 89

Answers (1)

Ed Morton
Ed Morton

Reputation: 204015

$ 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");

Upvotes: 4

Related Questions