-
-
Save lxr/19004d3b555294b7fc28 to your computer and use it in GitHub Desktop.
print tree structures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
{ | |
parent = $1 | |
child = $2 | |
if (!(parent in isroot)) | |
isroot[parent] = 1 | |
if (child != parent) | |
isroot[child] = 0 | |
n = ++childcount[parent] | |
childlist[parent,n] = child | |
} | |
END { | |
for (node in isroot) | |
if (isroot[node]) | |
treeprint("", node, "") | |
} | |
function treeprint(parent, node, indent, i, n) { | |
if (printed[node]) { | |
print indent "──", node, "->", printed[node] | |
return | |
} | |
n = childcount[node] | |
print indent "─" (n > 0 ? "┬" : "─"), node | |
printed[node] = parent | |
sub(/├$/, "│", indent) | |
sub(/└$/, " ", indent) | |
for (i = 1; i <= n; i++) | |
treeprint(node, childlist[node,i], indent (i == n ? " └" : " ├")) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NAME | |
tree - print tree structures | |
SYNOPSIS | |
tree [file ...] | |
DESCRIPTION | |
tree reads lines of parent node-child node pairs from each input | |
file and prints the graph structure they describe to standard output. | |
Despite its name, tree can be used to process arbitrary graphs; loops, | |
self-references, multiple parents, and multiple links between the same | |
pair of nodes are all supported. tree remembers the parent under which | |
every node was first encountered and prints out a reference to this node | |
whenever the child is encountered a second time, so every edge is | |
travelled only once. | |
SEE ALSO | |
tsort(1) | |
BUGS | |
tree uses UTF-8 box drawing characters to illustrate the hierarchy, so | |
its output is likely to be garbled on serial consoles. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment