:
fooobar.com/questions/1567922/...
node , . , , , k1 k2, k2 k1 , :
$ cat tst.awk
function descend(node, child, descendants) {
stack[node]
child = map[node]
if (child in map) {
if (child in stack) {
descendants = node "*"
}
else {
descendants = child " " descend(child)
}
}
else {
descendants = child
}
delete stack[node]
return descendants
}
{ keys[++numKeys] = $1; map[$1] = $2 }
END {
for (keyNr=1; keyNr<=numKeys; keyNr++) {
key = keys[keyNr]
print key, descend(key)
}
}
.
$ awk -f tst.awk file
k1 v1
k2 v2
k3 v3
k4 k1 v1
k5 k4 k1 v1
k1 v1, k1 x1 - , 2D- , , 1D map , - :
{
if (!seen[$1]++) {
keys[++numKeys] = $1
}
map[$1,++cnt[$1]] = $2
}
descend() []
for (i=1; i<=cnt[node]; i++) {
child = map[node,i]
if (child in map) {
...
}
}
:
child = map[node]
if (child in map) {
...
}
This may not be perfect because it is untested, but it is the right idea and will not be too complicated for you to debug (i.e. I am not going to!).
source
share