#!/bin/bash
>r.txt
OLD_IFS=$IFS
export IFS=","
sentence='"衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"'
sentence2='5, 20, 36, 10, 10, 20'
s_arr=($sentence)
s2_arr=($sentence2)
IFS="$OLD_IFS"
IFS=";"
template=" {name:var1,value:var2},"
for i in ${!s_arr[@]};
do
var1=${s_arr[$i]}
var2=${s2_arr[$i]}
# sed 's/var1/$var1/g' $template | sed 's/var2/$var2/g'>>r.txt
echo $template |sed "s/var1/$var1/g" | sed "s/var2/$var2/g" >>r.txt
done
主要注意分隔符IFS,否则template的逗号或者空格都会没有了
结果为
{name:"衬衫",value:5},
{name:"羊毛衫",value: 20},
{name:"雪纺衫",value: 36},
{name:"裤子",value: 10},
{name:"高跟鞋",value: 10},
{name:"袜子",value: 20},