Loop through a comma-separated shell variable

前端 未结 8 1895
醉话见心
醉话见心 2020-12-22 18:15

Suppose I have a Unix shell variable as below

variable=abc,def,ghij

I want to extract all the values (abc, def an

相关标签:
8条回答
  • 2020-12-22 18:50

    Here's my pure bash solution that doesn't change IFS, and can take in a custom regex delimiter.

    loop_custom_delimited() {
        local list=$1
        local delimiter=$2
        local item
        if [[ $delimiter != ' ' ]]; then
            list=$(echo $list | sed 's/ /'`echo -e "\010"`'/g' | sed -E "s/$delimiter/ /g")
        fi
        for item in $list; do
            item=$(echo $item | sed 's/'`echo -e "\010"`'/ /g')
            echo "$item"
        done
    }
    
    0 讨论(0)
  • 2020-12-22 18:55

    Try this one.

    #/bin/bash   
    testpid="abc,def,ghij" 
    count=`echo $testpid | grep -o ',' | wc -l` # this is not a good way
    count=`expr $count + 1` 
    while [ $count -gt 0 ]  ; do
         echo $testpid | cut -d ',' -f $i
         count=`expr $count - 1 `
    done
    
    0 讨论(0)
  • 2020-12-22 18:56

    Another solution not using IFS and still preserving the spaces:

    $ var="a bc,def,ghij"
    $ while read line; do echo line="$line"; done < <(echo "$var" | tr ',' '\n')
    line=a bc
    line=def
    line=ghij
    
    0 讨论(0)
  • 2020-12-22 18:59

    Here is an alternative tr based solution that doesn't use echo, expressed as a one-liner.

    for v in $(tr ',' '\n' <<< "$var") ; do something_with "$v" ; done
    

    It feels tidier without echo but that is just my personal preference.

    0 讨论(0)
  • 2020-12-22 19:03

    You can use the following script to dynamically traverse through your variable, no matter how many fields it has as long as it is only comma separated.

    variable=abc,def,ghij
    for i in $(echo $variable | sed "s/,/ /g")
    do
        # call your procedure/other scripts here below
        echo "$i"
    done
    

    Instead of the echo "$i" call above, between the do and done inside the for loop, you can invoke your procedure proc "$i".


    Update: The above snippet works if the value of variable does not contain spaces. If you have such a requirement, please use one of the solutions that can change IFS and then parse your variable.


    Hope this helps.

    0 讨论(0)
  • 2020-12-22 19:07

    Not messing with IFS
    Not calling external command

    variable=abc,def,ghij
    for i in ${variable//,/ }
    do
        # call your procedure/other scripts here below
        echo "$i"
    done
    

    Using bash string manipulation http://www.tldp.org/LDP/abs/html/string-manipulation.html

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