BASH | read -a variable <<< $dataset (Change delimiter)

Discussion in 'Programming/Scripts' started by thisiszeev, Jan 21, 2023 at 10:22 PM.

Tags:
  1. thisiszeev

    thisiszeev New Member

    The data set I am working with has spaces.

    Structure:
    SUBURB TOWN NUM1 NUM2

    Since Suburbs and Towns often have Spaces in i have chosen to use the typical CSV structure.

    VALLEY VIEW,JONES TOWN,2032,2033

    Using the read command I can easily save it into variables using the dataset but the issue is that read -a defaults to a space character as the delimiter, so the above becomes...

    ${variable[0]} is VALLEY
    ${variable[1]} is VIEW,JONES
    ${variable[2]} is TOWN,2032,2033
    ${variable[3]} is null

    I need to use the comma character as the delimiter so I can get the following...

    ${variable[0]} is VALLEY VIEW
    ${variable[1]} is JONES TOWN
    ${variable[2]} is 2032
    ${variable[3]} is 2033

    I tried using ...

    read -d "," -a variable <<< $dataset



    No difference. Any one got some idea?
     
  2. thisiszeev

    thisiszeev New Member

    Figured it out.
    Use...
    IFS=','
    read -a variable <<< $dataset
    unset IFS
     

Share This Page