# Use character from node ID as label

**URL:** <https://forum.graphviz.org/t/use-character-from-node-id-as-label/1659>\
**Category:** Help\
**Created:** [June 13, 2023, 6:13pm UTC](https://forum.graphviz.org/t/use-character-from-node-id-as-label/1659 "2023-06-13T18:13:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dot](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@dot](https://forum.graphviz.org/u/dot)\
**Post date:** [June 13, 2023, 6:13pm UTC](https://forum.graphviz.org/t/use-character-from-node-id-as-label/1659/1 "2023-06-13T18:13:34Z")

</div>

# Question

Is there a way to use part of the node ID in label?

In the example below, I’d like to use the last character. The  
alternative solution would remove the prefix `x`.

# MWE

```graphviz
digraph x {
  node [shape="square"]
  x0
  x1
  x2
  x3
  x4
  x5
  x6
  x7
}

```

# Have

![Have](https://global.discourse-cdn.com/graphviz/original/2X/7/7d0fb0d635b711aa9efdbf4f6582efce636f0cba.png)

# Want

![Want](https://global.discourse-cdn.com/graphviz/original/2X/6/6d4ed5b511edcc31a474aa1c3e9fe15fad19e137.png)

# Notes

Simply removing the prefix `x` in the node name is not a solution for more complex graphs.

I was thinking there might be some syntax that can be combined with `\N` (e.g., `\N[-1]`).

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [June 13, 2023, 10:39pm UTC](https://forum.graphviz.org/t/use-character-from-node-id-as-label/1659/2 "2023-06-13T22:39:08Z")

</div>

Does this `x5 [label=5]` not work for you - i.e. generate each node’s **label** explicitly?

If not, no there is nothing in the Graphviz language similar to what you want.  
However, the following **gvpr** ([https://www.graphviz.org/pdf/gvpr.1.pdf](https://www.graphviz.org/pdf/gvpr.1.pdf)) one-liner will strip the all alpha characters off each node name & assign the result to the node label. More string manipulation is available. (_Note_, **gvpr** uses **ksh/bash** pattern-matching syntax!)

```auto
gvpr -c 'N{$.label=sub($.name,"+([a-zA-Z])");}' myfile.gv >myfileNew.gv

```

Giving:

```plaintext
	graph [rankdir=LR];
	node [label=""];
	edge [style=invis];
	subgraph subset {
		node [shape=none];
		s0	[label=0,
			shape=square];
		s1	[label=1];
		s0 -> s1;
		s3	[label=3,
			shape=square];
		s4	[label=4];
		s3 -> s4;
		s5	[label=5,
			shape=square];
		s6	[label=6,
			shape=square];
		s5 -> s6;
		s7	[label=7];
		s6 -> s7;
		s2	[label=2];
		s1 -> s2;
		s2 -> s3;
		s4 -> s5;
	}
	subgraph fullset {
		node [shape=square];
		fs0	[label=0];
		fs1	[label=1];
		fs0 -> fs1;
		fs2	[label=2];
		fs1 -> fs2;
		fs3	[label=3];
		fs2 -> fs3;
		fs4	[label=4];
		fs3 -> fs4;
		fs5	[label=5];
		fs4 -> fs5;
		fs6	[label=6];
		fs5 -> fs6;
		fs7	[label=7];
		fs6 -> fs7;
	}
}

```

Note that directly manipulating a **label** value containing escape-string characters is more work. The Graphviz parsers do not show expanded escape-chars until very late in the game - see:

```auto
dot -Tdot myfile.gv

```

note that \N is not shown expanded
