我需要生成很多表格信。
$>cat debug-form-letter
The first field is $1, the second $2, the third $3,
the 10th field is $10,but the 10th correct value is varA.
$> cat debug-replace-value
var1|var2|var3|var4|var5|var6|var7|var8|var9|varA|varB
$> cat debug-replace-form-letter.awk
BEGIN {FS = "|"
while (getline <"debug-form-letter")
line[++n] = $0
}
{for (i = 1; i <= n; i++) {
s = line[i]
for (j = 1; j <= NF; j++)
gsub("\\$"j, $j, s)
print s
}
}
- 我致电
$> awk -f debug-replace-form-letter.awk debug-replace-value
- 10我希望得到这样的
The first field is var1, the second var2, the third var3,
the 10th field is varA,but the 10th correct value is varA.
- 20但我得到了这个
The first field is var1, the second var2, the third var3,
the 10th field is var10,but the 10th correct value is varA.
以上$ 10是不正确的,它变成$ 1加0,我尝试双引号 单引号,它也不起作用。
AND $ 11变为$ 1加1。
我的awk是4.1.3,我更新到最新版本它也没有用。
$> awk -V
GNU Awk 4.1.3, API: 1.1
Copyright (C) 1989, 1991-2015 Free Software Foundation.
我的剧本出了什么问题?我怎样才能让它发挥作用?
答案 0 :(得分:0)
将内部for
循环更改为
for (j = NF; j >= 1; j--)
在您的情况下,$1
在$10
有机会匹配之前匹配。