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?
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With