Split string into an array in Bash

后端 未结 22 2285
故里飘歌
故里飘歌 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:43

    We can use tr command to split string into the array object. It works both MacOS and Linux

      #!/usr/bin/env bash
      currentVersion="1.0.0.140"
      arrayData=($(echo $currentVersion | tr "." "\n"))
      len=${#arrayData[@]}
      for (( i=0; i<=$((len-1)); i++ )); do 
           echo "index $i - value ${arrayData[$i]}"
      done
    

    Another option use IFS command

    IFS='.' read -ra arrayData <<< "$currentVersion"
    #It is the same as tr
    arrayData=($(echo $currentVersion | tr "." "\n"))
    
    #Print the split string
    for i in "${arrayData[@]}"
    do
        echo $i
    done
    
    0 讨论(0)
  • 2020-11-22 04:43

    For multilined elements, why not something like

    $ array=($(echo -e $'a a\nb b' | tr ' ' '§')) && array=("${array[@]//§/ }") && echo "${array[@]/%/ INTERELEMENT}"
    
    a a INTERELEMENT b b INTERELEMENT
    
    0 讨论(0)
  • 2020-11-22 04:43

    Another way would be:

    string="Paris, France, Europe"
    IFS=', ' arr=(${string})
    

    Now your elements are stored in "arr" array. To iterate through the elements:

    for i in ${arr[@]}; do echo $i; done
    
    0 讨论(0)
  • 2020-11-22 04:43

    Another approach can be:

    str="a, b, c, d"  # assuming there is a space after ',' as in Q
    arr=(${str//,/})  # delete all occurrences of ','
    

    After this 'arr' is an array with four strings. This doesn't require dealing IFS or read or any other special stuff hence much simpler and direct.

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

    Here is a way without setting IFS:

    string="1:2:3:4:5"
    set -f                      # avoid globbing (expansion of *).
    array=(${string//:/ })
    for i in "${!array[@]}"
    do
        echo "$i=>${array[i]}"
    done
    

    The idea is using string replacement:

    ${string//substring/replacement}
    

    to replace all matches of $substring with white space and then using the substituted string to initialize a array:

    (element1 element2 ... elementN)
    

    Note: this answer makes use of the split+glob operator. Thus, to prevent expansion of some characters (such as *) it is a good idea to pause globbing for this script.

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

    Since there are so many ways to solve this, let's start by defining what we want to see in our solution.

    1. Bash provides a builtin readarray for this purpose. Let's use it.
    2. Avoid ugly and unnecessary tricks such as changing IFS, looping, using eval, or adding an extra element then removing it.
    3. Find a simple, readable approach that can easily be adapted to similar problems.

    The readarray command is easiest to use with newlines as the delimiter. With other delimiters it may add an extra element to the array. The cleanest approach is to first adapt our input into a form that works nicely with readarray before passing it in.

    The input in this example does not have a multicharacter delimiter. If we apply a little common sense, it's best understood as comma separated input for which each element may need to be trimmed. My solution is to split the input by comma into multiple lines, trim each element, and pass it all to readarray.

    string='  Paris,France  ,   All of Europe  '
    readarray -t foo < <(tr ',' '\n' <<< "$string" |sed 's/^ *//' |sed 's/ *$//')
    declare -p foo
    
    # declare -a foo='([0]="Paris" [1]="France" [2]="All of Europe")'
    
    0 讨论(0)
提交回复
热议问题