Search code examples
arraysbashmacosifs

Splitting string to array in bash with long delimiter


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.


Solution

  • 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 array