Use awk to split line into array and use that array's values in calling shell
I think that you can do what you want without awk:
cat "${smryfile}" | while IFS=: read first last varx
do
echo "first='$first' last='$last' varx='$varx'"
# do something
done
This produces:
first='Joe' last='Johnson' varx='25'
first='Sue' last='Miller' varx='27'
Note that this approach will work even if some names in the file include spaces.
Note also that the use of cat
above is not necessary:
while IFS=: read first last varx
do
echo "first='$first' last='$last' varx='$varx'"
# do something
done <"${smryfile}"
A side benefit of removing cat
, as per the above, is that any variables that you create in the loop will survive after the loop finishes.
This should work:
linearray=($(awk -F: '{$1=$1} 1' <<<"${smryline}"))
echo ${linearray[2]}
# output: 27
Explanation: awk -F:
splits input on :
. awk
by default separates modified output with a space, so you can construct an bash array directly with the output from awk
. Note modified output, hence the no-op call to $1=$1
, else the data would just come out in the original form.
But given your example, why not extract the third column with awk -F:
and loop the output:
awk -F: '{print $3}' "$smryfile" | while read varX; do
echo $varX
done