0

This question already has an answer here:

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

marked as duplicate by don_crissti, Community Jan 10 '17 at 21:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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"))

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