Suppose I have a Unix shell variable as below
假设我有一个Unix shell变量,如下所示
variable=abc,def,ghij
I want to extract all the values (abc
, def
and ghij
) using a for loop and pass each value into a procedure.
我想使用for循环提取所有值(abc,def和ghij)并将每个值传递给过程。
The script should allow extracting arbitrary number of comma-separated values from $variable
.
该脚本应允许从$ variable中提取任意数量的逗号分隔值。
5 个解决方案
#1
55
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"
.
而不是上面的回声“$ i”调用,在for循环中的do和done之间,你可以调用你的过程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.
更新:如果变量的值不包含空格,则上述代码段有效。如果您有这样的要求,请使用可以更改IFS的解决方案之一然后解析您的变量。
Hope this helps.
希望这可以帮助。
#2
69
Not messing with IFS
Not calling external command
不搞乱IFS不调用外部命令
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
使用bash字符串操作http://www.tldp.org/LDP/abs/html/string-manipulation.html
#3
27
If you set a different field separator, you can directly use a for
loop:
如果设置不同的字段分隔符,则可以直接使用for循环:
IFS=","
for v in $variable
do
# things with "$v" ...
done
You can also store the values in an array and then loop through it as indicated in How do I split a string on a delimiter in Bash?:
您还可以将值存储在数组中,然后按照如何在Bash中的分隔符上拆分字符串中所示循环它:
IFS=, read -ra values <<< "$variable"
for v in "${values[@]}"
do
# things with "$v"
done
Test
$ variable="abc,def,ghij"
$ IFS=","
$ for v in $variable
> do
> echo "var is $v"
> done
var is abc
var is def
var is ghij
You can find a broader approach in this solution to How to iterate through a comma-separated list and execute a command for each entry.
您可以在此解决方案中找到更广泛的方法,如何迭代逗号分隔列表并为每个条目执行命令。
Examples on the second approach:
第二种方法的例子:
$ IFS=, read -ra vals <<< "abc,def,ghij"
$ printf "%s\n" "${vals[@]}"
abc
def
ghij
$ for v in "${vals[@]}"; do echo "$v --"; done
abc --
def --
ghij --
#4
2
#/bin/bash
TESTSTR="abc,def,ghij"
for i in $(echo $TESTSTR | tr ',' '\n')
do
echo $i
done
I prefer to use tr instead of sed, becouse sed have problems with special chars like \r \n in some cases.
我更喜欢使用tr而不是sed,因为在某些情况下,像\ r \ n这样的特殊字符有问题。
other solution is to set IFS to certain separator
其他解决方案是将IFS设置为某个分隔符
#5
0
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
#1
55
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"
.
而不是上面的回声“$ i”调用,在for循环中的do和done之间,你可以调用你的过程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.
更新:如果变量的值不包含空格,则上述代码段有效。如果您有这样的要求,请使用可以更改IFS的解决方案之一然后解析您的变量。
Hope this helps.
希望这可以帮助。
#2
69
Not messing with IFS
Not calling external command
不搞乱IFS不调用外部命令
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
使用bash字符串操作http://www.tldp.org/LDP/abs/html/string-manipulation.html
#3
27
If you set a different field separator, you can directly use a for
loop:
如果设置不同的字段分隔符,则可以直接使用for循环:
IFS=","
for v in $variable
do
# things with "$v" ...
done
You can also store the values in an array and then loop through it as indicated in How do I split a string on a delimiter in Bash?:
您还可以将值存储在数组中,然后按照如何在Bash中的分隔符上拆分字符串中所示循环它:
IFS=, read -ra values <<< "$variable"
for v in "${values[@]}"
do
# things with "$v"
done
Test
$ variable="abc,def,ghij"
$ IFS=","
$ for v in $variable
> do
> echo "var is $v"
> done
var is abc
var is def
var is ghij
You can find a broader approach in this solution to How to iterate through a comma-separated list and execute a command for each entry.
您可以在此解决方案中找到更广泛的方法,如何迭代逗号分隔列表并为每个条目执行命令。
Examples on the second approach:
第二种方法的例子:
$ IFS=, read -ra vals <<< "abc,def,ghij"
$ printf "%s\n" "${vals[@]}"
abc
def
ghij
$ for v in "${vals[@]}"; do echo "$v --"; done
abc --
def --
ghij --
#4
2
#/bin/bash
TESTSTR="abc,def,ghij"
for i in $(echo $TESTSTR | tr ',' '\n')
do
echo $i
done
I prefer to use tr instead of sed, becouse sed have problems with special chars like \r \n in some cases.
我更喜欢使用tr而不是sed,因为在某些情况下,像\ r \ n这样的特殊字符有问题。
other solution is to set IFS to certain separator
其他解决方案是将IFS设置为某个分隔符
#5
0
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