I am trying to write a simple bash script that can extract data from one column in csv file and store it as an array. My question is very similar to that of a previous post, but I am trouble getting the proposed solution from that post to work (possibly because my CSV file has headers).
Specifically, I have a CSV file, weights.csv, with two columns:
w_neg,w_pos
1.000,1.000
0.523,1.477
0.210,1.790
1.420,0.580
and I would like to create an array variable, w_pos, that will contain the entire second column of weights.csv.
w_pos=(1.000 1.477 1.790 0.580)
Based on the answer from this previous post, I tried to do this using the following line of code:
w_pos=( $(cut -d ',' -f2 weights.csv ) )
Unfortunately, it seems as if this only stores the first row of w_pos. As
echo ${w_pos[0]}
1.000
but
echo ${w_pos[1]}
yields nothing.
I would appreciate any insight into what the problem might be. Ideally, I would like a solution that does not use packages other than what would be bundled with a barebones Unix installation (the script has to run on a cluster that doesn't have simple tools like "bc" :-/)
${w_pos[0]}should bew_poswhile${w_pos[1]}should be1.000instead. What do you see for${w_pos[*]}?