shell如何将字符串按逗号分割并放入数组
主要是分割的问题,用以下方法老是按空格和逗号一起分割#!/bin/bashstring="hello,shell,haha"OLD_IFS="$IFS"IFS=","ar...
主要是分割的问题,用以下方法老是按空格和逗号一起分割
#!/bin/bash
string="hello,shell,haha"
OLD_IFS="$IFS"
IFS=","
array=($string)
IFS="$OLD_IFS"
for var in ${array[@]}
do
echo $var
done 展开
#!/bin/bash
string="hello,shell,haha"
OLD_IFS="$IFS"
IFS=","
array=($string)
IFS="$OLD_IFS"
for var in ${array[@]}
do
echo $var
done 展开
1个回答
展开全部
root@localhost:~/xly# sh 1 hello
abc
shell
haha
1 hello abc
2 shell
3 haha
root@localhost:~/xly# vim 1
#!/bin/bash
string="hello abc,shell,haha"
OLD_IFS="$IFS"
IFS=","
array=($string)
IFS="$OLD_IFS"
for var in ${array[@]}
do
echo $var
done
echo $string | awk '{split($0,arr,",");for(i in arr) print i,arr[i]}'
用awk更加精准
abc
shell
haha
1 hello abc
2 shell
3 haha
root@localhost:~/xly# vim 1
#!/bin/bash
string="hello abc,shell,haha"
OLD_IFS="$IFS"
IFS=","
array=($string)
IFS="$OLD_IFS"
for var in ${array[@]}
do
echo $var
done
echo $string | awk '{split($0,arr,",");for(i in arr) print i,arr[i]}'
用awk更加精准
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询