我在脚本中使用以下行。
foo=$1
bar=$(printf ' , "%s" u ($1):($2) lw 8 ti' ${foo[@]} ${foo[@]} ${foo[@]} ${foo[@]} )
bar=${bar:2}
echo $bar
这会产生以下行作为变量bar的值
"rdf_inter_fortran_05-25.xvg" u ($1):($2) lw 8 ti , "rdf_inter_fortran_05-25.xvg" u ($1):($2) lw 8 ti , "rdf_inter_fortran_05-25.xvg" u ($1):($2) lw 8 ti , "rdf_inter_fortran_05-25.xvg" u ($1):($2) lw 8 ti
是否可以修改脚本以使输出为:
"rdf_inter_fortran_05-25.xvg" u ($1):($2) lw 8 ti , "rdf_inter_fortran_05-25.xvg" u ($1):($3) lw 8 ti , "rdf_inter_fortran_05-25.xvg" u ($1):($4) lw 8 ti , "rdf_inter_fortran_05-25.xvg" u ($1):($5) lw 8 ti
答案 0 :(得分:1)
将printf行更改为以下内容:
bar=$(printf ' , "%s" u ($1):($%s) lw 8 ti' ${foo[@]} "2" ${foo[@]} "3" ${foo[@]} "4" ${foo[@]} "5" )
答案 1 :(得分:0)
看起来你用新的字符串替换了2美元。在echo $bar
:
bar=$(echo $bar | sed 's/$2/$5/4; s/$2/$4/3; s/$2/$3/2;')
这将取代$ 2的第4次,第3次和第2次,新的价值分别为5美元,4美元和3美元。