我想做类似的事情:
for (x in {a,b}) {
...
}
有没有办法在awk中执行此操作?
答案 0 :(得分:3)
两个选项:
1)
for (x in a)
u[x]
for (x in b)
u[x]
for (x in u)
print "Union Index:",x
2)
for (x in a)
print "Union Index:",x
for (x in b)
if (!(x in a))
print "Union Index:",x
如果你想要一些你可以在for循环中简洁地使用的东西:
$ cat tst.awk
function indices(a,b,u, x,c)
{
for (x in a) {
u[++c] = x
}
for (x in b) {
if (!(x in a)) {
u[++c] = x
}
}
return c
}
BEGIN {
a[3]="foo"
a[9]=3
b[5]=7
b[15]=45
for (i=1; i<=indices(a,b,u); i++) {
print u[i]
}
}
$ awk -f tst.awk
9
3
5
15