Thanks @steveroush , that is great start. I knew my dormant AWK skills are there for something.
I’m attaching a refined version of your script. It handles \N as labels for nodes and also keeps edge labels.
/* vim: set ft=awk:
*
* Generate copy of input graph, replacing names with labels
* NOTE: DOES NOT COPY SUBGRAPHS!
* NOTE: ugly if you use record OR html nodes
*/
BEGIN {
int id = 0;
int cnt[];
string names[string];
string mapn (string inname, string lbl)
{
string s;
s = names[inname];
/* print("// mapn returning: >", s, "< with inname >", inname, "< and lbl >", lbl, "<"); */
if(!strcmp(lbl,""))
{
print("// Error:: node ", inname, " has no label");
s = inname;
}
else
{
if(!strcmp(lbl,"\\N"))
{
lbl = inname;
}
}
if(cnt[lbl] > 0)
{
print("// Error:: duplicate label >", lbl, "<");
s = inname;
}
else
{
if(!strcmp(s, ""))
{
s = lbl;
cnt[lbl] = 1;
}
}
names[inname] = s;
return s;
}
string getmapn (string inname)
{
string s;
s = names[inname];
if (s == "") {
print("// Error:: Edge had mapping problem with: ", inname);
}
return s;
}
}
BEG_G {
graph_t g;
node_t aNode;
g = copy (NULL, $G);
for (aNode = fstnode($G);aNode;aNode = nxtnode(aNode)){
node (g, mapn(aNode.name, aNode.label));
}
}
E {
edge_t e = edge (node (g, getmapn($.tail.name)), node (g, getmapn($.head.name)), "");
e.label = $.label;
}
END_G {
write (g);
}
My test input
digraph g {
n0 [label=A];
n1 [label=B];
C [label="\N"];
n0 -> n1 [label = "Yes"];
n1 -> C [label = "No"];
}