0

I have a variable animals.

animals=lion*tiger*elephant**cat***dog

I just want to split by the delimiter * and store it into an array.

 Expected:

 animals[0]="lion"
 animals[1]="tiger"
 animals[2]="elephant"
 animals[3]=""
 animals[4]="cat"
 animals[5]=""
 animals[6]=""
 animals[7]="dog"

I used awk command but I don't know how to store empty string for no value.

echo "$(awk -F* '{ for (i = 1; i <= 8; i++) print $i }' <<< "$animals")"

Result:

lion
tiger
elephant

cat


dog

Array:

Array=($(awk -F* '{ for (i = 1; i <= 8; i++) print $i }' <<< "$animals")) 
# Null values get neglected
0

I replaced as null for no values,

Array=($(awk -F* '{ for (i = 2; i <= 10; i++) { if ($i==""){print "null"} else {print $i} }  }' <<< "$animals"))
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.