8

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?

1
  • 1
    Remove your IFS assignment. Commented Sep 7, 2013 at 4:47

4 Answers 4

14

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.

Sign up to request clarification or add additional context in comments.

4 Comments

this answer will met with trouble if blank space exist, i.e.:New York
Won't this break if the CSV has quoted fields with commas inside them?
Yes, spaces in the fields would be a problem. But the data in the question does not contain spaces. I was shooting for the simplest thing that would work.
I understand that this answer solves the problem and that why it has so many upvotes but it lacks a proper explanation of the syntax which makes it hard for beginners to learn from it.
11

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]}"

1 Comment

Thanks Aleks, I will try your proposed solutions too.
10

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]}"

1 Comment

in the end IFS gave me a hard time because of one my columns contained data with blank spaces. I then tried your proposed approach, it worked like a charm! Thanks man!
0

i like to use awk. First split based on , delimiter and take required column values into array.

abc=($(tail -n +1 MyAssignment.csv | awk -F ',' '{print $2;}'))
echo ${abc[1]}

Index start from 1. If the file contains headers, replace +1 with +2 to ignore headers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.