If I have this awk command...
echo $line | awk '{split($0,array,"|")}'
...how can I use array later in the bash program? If I try to print out information from the array later it's just empty.
An other solution in bash if I presume that fields haven't got space then only 1 assignment.
ln="apple|lemon|orange|strawberry" # eg
v=(${ln//|/ }) # substitute | with " " and vectorising with ()
# Now we are ready
# checking it:
for((i=0;i<${#v[@]};i++));do echo ${v[$i]}; done
apple
lemon
orange
strawberry
#or:
for i in ${v[@]}; do echo $i; done
apple
lemon
orange
strawberry
If we have some space but no underline "_" we need 3 steps.
ln="api ple|l em on|ora nge|s traw berry"
echo "$ln"
api ple|l em on|ora nge|s traw berry
ln=${ln// /_} # 1st
echo "$ln"
api_ple|l_em__on|ora__nge|s_traw___berry
v=(${ln//|/ }) # 2nd
for i in ${v[@]}; do echo $i; done
api_ple
l_em__on
ora__nge
s_traw___berry
for((i=0;i<${#v[@]};i++));do v[$i]="${v[i]//_/ }"; done # 3rd
for((i=0;i<${#v[@]};i++));do echo "${v[$i]}"; done
api ple
l em on
ora nge
s traw berry
bashVars=$(echo "$line" | awk '{n=split($0,arr,"|"); print arr[1] " " arr[n] }' ). for example will print the first and last value from each line and assign to a single shell varbashVar. You can get fancy and declare that var as an arr var if need be. Good luck.