Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH - How to extract data from a column in CSV file and put it in an array?

Tags:

arrays

bash

csv

I am learning to script in Bash.

I have a CSV file with 5 columns and 22 rows. I am interested in taking the data from the second column and put it into an array.

What I want is that the first name be in array[0], the second in array[1] and so on.

Bash script:

#!/bin/sh
IFS=","
eCollection=( $(cut -d ',' -f2 MyAssignment.csv ) )
printf "%s\n" "${eCollection[0]}"

The CSV looks like this. There is no line with headers.

The column with the Vl18xx numbers is what I want to split into an array.

John,Vl1805,VRFname,10.9.48.64/28,10.9.48.78 
John,Vl1806,VRFname,10.9.48.80/28,10.9.48.94
John,Vl1807,VRFname,10.9.48.96/28,10.9.48.110 
John,Vl1808,VRFname,10.9.48.112/28,10.9.48.126
John,Vl1809,VRFname,167.107.152.32/28,167.107.152.46

The bash script is not placing the 2nd column into the array, what am I doing wrong?

like image 901
DMS Avatar asked Sep 07 '13 04:09

DMS


3 Answers

Remove the IFS="," assignment thereby letting the default IFS value of space, tab, newline apply

#!/bin/bash

eCollection=( $(cut -d ',' -f2 MyAssignment.csv ) )
printf "%s\n" "${eCollection[0]}"

Explained: The eCollection variable is an array due to the outer parenthesis. It is initialized with each element from the IFS-separated (think function or command line arguments) words which come from the $(cut -d ',' -f2 MyAssignment.csv) subshell, which is the cut command used with the ',' delimiter and printing the second field -f2 from the MyAssignment.csv file.

The printf statement just shows how to print any item by index, you could also try echo "${eCollection[@}]}" to see all of the elements.

like image 143
Adam Burry Avatar answered Oct 14 '22 11:10

Adam Burry


Two pure bash solutions:

eCollection=()
while IFS=',' read -r _ second _; do
    eCollection+=("$second")
done < file.txt
printf '%s\n' "${eCollection[0]}"


readarray -t eCollection < file.txt
eCollection=("${eCollection[@]#*,}")
eCollection=("${eCollection[@]%%,*}")
printf '%s\n' "${eCollection[0]}"
like image 11
Aleks-Daniel Jakimenko-A. Avatar answered Oct 14 '22 12:10

Aleks-Daniel Jakimenko-A.


Better is to use readarray. No need to mind about IFS which could have any value, and is safe from pathname expansion.

readarray -t eCollection < <(cut -d, -f2 MyAssignment.csv)
printf '%s\n' "${eCollection[0]}"
like image 10
konsolebox Avatar answered Oct 14 '22 10:10

konsolebox