例如一个字符串为:hua nong jing chao,我想以空格为分隔符把次字符串分开,并把分割后的字符串赋值给4个变量var1,var2,var3,var4
方法如下:
var1=`echo “hua nong jing chao”|awk -F ‘ ‘ ‘{print $1}’`
echo $var1
var2=`echo “hua nong jing chao”|awk -F ‘ ‘ ‘{print $2}’`
echo $var2
var3=`echo “hua nong jing chao”|awk -F ‘ ‘ ‘{print $3}’`
echo $var3
var4=`echo “hua nong jing chao”|awk -F ‘ ‘ ‘{print $4}’`
echo $var4
注:如果想用其他字符作为分隔符,则-F后面可以换成相应的分割付,然后进行分割