Reputation: 1011
Using bash in Terminal on OSX, this code:
newtext="FIRST<br><br>SECOND<br><br>THIRD"
IFS='<br><br>' read -ra text_array <<< "$newtext"
printf "%s\n" "${text_array[@]}"
outputs:
FIRST
SECOND
THIRD>
Why does the array have so many newline elements? If I use single-character delimiters it works.
Upvotes: 3
Views: 152
Reputation: 784898
IFS
can split string on each of the single character only and when you provide <br><br>
it splits input on <
, b
, r
and >
.
You can use this work-around to get your job done:
s="FIRST<br><br>SECOND<br><br>THIRD"
del=$'\5' # our arbitrary delimiter; can be any other control character
IFS="$del" read -ra arr <<< "${s//<br><br>/$del}"
declare -p arr
declare -a arr='([0]="FIRST" [1]="SECOND" [2]="THIRD")'
del=$'\5'
sets delimiter to control character \5
. \5
is just an arbitrary character and it can be replaced with any other character that is not present in input."${s//<br><br>/$del}"
replace <br><br>
with control character \5
IFS="$del"
splits input on \5
while populating arrayUpvotes: 5