views:

139

answers:

5

I'm trying to create an array in bash from a file with the following sample format:

data, data, interesting
data, data, more interesting

The way I'm populating the arrays:

read -r -a DATA1 <<< $(cat $FILE | awk -F, '{ print $1 }')
read -r -a DATA2 <<< $(cat $FILE | awk -F, '{ print $2 }')
read -r -a DATA3 <<< $(cat $FILE | awk -F, '{ print $3 }')

When I examine the array DATA3, there are 3 elements:

interesting
more
interesting

I need it to show only 2 elements like:

interesting
more interesting

How can I preserve the white space in field 3 so when I call that element from the array, it appears as "more interesting"? Is there a better way to handle this?

A: 

Easier to use sed then awk, Something like this to extract everything after the second comma:

cat test.txt|sed -e 's/.*,.*,//'
Thilo
+1  A: 

Use cut. Example:

read -r -a DATA1 <<< $(cut -d, -f 1 $FILE)
read -r -a DATA2 <<< $(cut -d, -f 2 $FILE)
read -r -a DATA3 <<< $(cut -d, -f 3 $FILE)
Emil Vikström
A: 

The key is to use the IFS (internal field separators) variable together with read.

read can read words directly into an array using the -a option. Set IFS=, and it will split at comma:

while read f; do
    echo "$f" | (IFS=, && read -a arr && echo ${arr[2]})
done

will echo

interesting
 more interesting 

You can also read directly into variable names:

IFS=, && read f1 f2 f2

EDIT: I can recommend reading Advanced Bash Scripting Guide.

JesperE
+1  A: 
IFS=','
while read -a array
do
    echo "${array[2]}" 
done < data.csv
Dennis Williamson
A: 
>> Is there a better way to handle this? - Tee Bone

$ awk -F", " '{print $3}' file
interesting
more interesting
ghostdog74