使用printf并逐步增加字符串变量?

时间:2017-04-03 22:34:14

标签: awk

我正在尝试输出一个字符串但是我需要在字符串前面加一个数字来逐渐增加直到它结束。我从txt文件中提取数据。

/Circle/{n=split($2,array,",")
if (index(array[2],"x")){x=array[3]}
else if (index(array[2],"y")){y=array[3]}
else if (index(array[2],"z")){z=array[3]}
else if (index(array[2],"r")){z=array[3]}
else { printf("CIRCLE/%.4f,%.4f,%.4f,%.4f\n", x, y, r, z) }}

这就是我目前所拥有的,它的输出就像这样

CIRCLE / 0.0000,0.0000,0.0000,1.0068

CIRCLE / 10.0000,0.0000,0.0000,1.9068

CIRCLE / 5.0000,0.0000,0.0000,1.8068

我需要它看起来像这样

CR1 = CIRCLE / 0.0000,0.0000,0.0000,1.0068

CR2 = CIRCLE / 10.0000,0.0000,0.0000,1.9068

CR3 = CIRCLE / 5.0000,0.0000,0.0000,1.8068

2 个答案:

答案 0 :(得分:2)

您没有发布任何示例输入,因此这显然未经测试,但您的代码可能看起来像这样:

/Circle/ {
    split($2,array,/,/)
    if (array[2] ~ /[xyzr]/) {
        v[array[2]] = array[3]
    }
    else {
        printf "CR%d=CIRCLE/%.4f,%.4f,%.4f,%.4f\n", ++cnt, v["x"], v["y"], v["r"], v["z"]
    }
}

答案 1 :(得分:0)

将printf更改为:

Dim dattime As DateTime = DateTime.FromOADate(data(row, 11))

(另外,不使用括号和print语句会更加惯用):

printf("%sCIRCLE/%.4f,%.4f,%.4f,%.4f\n", "CR" ++i "=", x, y, r, z)