Split string into an array in Bash

后端 未结 22 2268
故里飘歌
故里飘歌 2020-11-22 04:24

In a Bash script I would like to split a line into pieces and store them in an array.

The line:

Paris, France, Europe

I would like

相关标签:
22条回答
  • 2020-11-22 04:52

    Try this

    IFS=', '; array=(Paris, France, Europe)
    for item in ${array[@]}; do echo $item; done
    

    It's simple. If you want, you can also add a declare (and also remove the commas):

    IFS=' ';declare -a array=(Paris France Europe)
    

    The IFS is added to undo the above but it works without it in a fresh bash instance

    0 讨论(0)
  • 2020-11-22 04:55
    t="one,two,three"
    a=($(echo "$t" | tr ',' '\n'))
    echo "${a[2]}"
    

    Prints three

    0 讨论(0)
  • 2020-11-22 04:59

    I came across this post when looking to parse an input like: word1,word2,...

    none of the above helped me. solved it by using awk. If it helps someone:

    STRING="value1,value2,value3"
    array=`echo $STRING | awk -F ',' '{ s = $1; for (i = 2; i <= NF; i++) s = s "\n"$i; print s; }'`
    for word in ${array}
    do
            echo "This is the word $word"
    done
    
    0 讨论(0)
  • 2020-11-22 05:00

    Pure bash multi-character delimiter solution.

    As others have pointed out in this thread, the OP's question gave an example of a comma delimited string to be parsed into an array, but did not indicate if he/she was only interested in comma delimiters, single character delimiters, or multi-character delimiters.

    Since Google tends to rank this answer at or near the top of search results, I wanted to provide readers with a strong answer to the question of multiple character delimiters, since that is also mentioned in at least one response.

    If you're in search of a solution to a multi-character delimiter problem, I suggest reviewing Mallikarjun M's post, in particular the response from gniourf_gniourf who provides this elegant pure BASH solution using parameter expansion:

    #!/bin/bash
    str="LearnABCtoABCSplitABCaABCString"
    delimiter=ABC
    s=$str$delimiter
    array=();
    while [[ $s ]]; do
        array+=( "${s%%"$delimiter"*}" );
        s=${s#*"$delimiter"};
    done;
    declare -p array
    

    Link to cited comment/referenced post

    Link to cited question: Howto split a string on a multi-character delimiter in bash?

    0 讨论(0)
  • 2020-11-22 05:01

    The accepted answer works for values in one line.
    If the variable has several lines:

    string='first line
            second line
            third line'
    

    We need a very different command to get all lines:

    while read -r line; do lines+=("$line"); done <<<"$string"

    Or the much simpler bash readarray:

    readarray -t lines <<<"$string"
    

    Printing all lines is very easy taking advantage of a printf feature:

    printf ">[%s]\n" "${lines[@]}"
    
    >[first line]
    >[        second line]
    >[        third line]
    
    0 讨论(0)
  • 2020-11-22 05:03

    This is similar to the approach by Jmoney38, but using sed:

    string="1,2,3,4"
    array=(`echo $string | sed 's/,/\n/g'`)
    echo ${array[0]}
    

    Prints 1

    0 讨论(0)
提交回复
热议问题