0

I have a file say f1 in the following format:

digraph G {
  rankdir=TB;
  node [style="filled", ];
  2 [label="Decl n1", fillcolor="#FFEFD5", shape=box, ];
  3 [label="In1", fillcolor="#6495ED", shape=box, ];
  4 [label="Decl n2", fillcolor="#FFEFD5", shape=box, ];
  5 [label="In2", fillcolor="#6495ED", shape=box, ];
  ...........

  edge [dir=back, ];
  3 -> 2 [color="#000000", style="dotted", ];
  2 -> 3 [color="#000000", style="dotted", ];
  ...........
  }

I need to modify it such that each list is reversed and the "," at the end of the list should be removed. For example, the output format should be like:

2 [shape=box, fillcolor="#FFEFD5", label="Decl n1"];

How do I approach this problem? Do I have to use any scripts? I am not very familiar with shell scripts.

4
  • A library that understands the syntax is the safe place to start. See for instance pypi.python.org/pypi/graphviz (note, Python, not shell). Commented Jun 22, 2017 at 14:18
  • @CharlesDuffy Will I be able to get the graph object from dot file? Commented Jun 22, 2017 at 14:31
  • Yes, as described in the answers to How to parse a DOT file in Python Commented Jun 22, 2017 at 14:33
  • @CharlesDuffy, That helped alot. Thanks! Commented Jun 22, 2017 at 17:29

1 Answer 1

0

Ideally you'd use something which is aware of the Dot syntax, allowing you to find and fix invalid syntax reliably. There is a Dot plugin for IDEA which you could try (use F2 to move to the next problem in the file). For very simple (but risky, since it's not syntax-aware) replacements you can use sed:

sed -i 's/, \]/]/' your-file

As for reversing with sed, the general pattern would be:

sed -i 's/\(first-part\)\(second-part\)/\2\1/' your-file
Sign up to request clarification or add additional context in comments.

5 Comments

This only removes the extra "," at the end. It doesn't reverse the list?
Oh, I didn't see the reversal requirement. You can do that by using sed with backreferences (\1 etc).
Can you elaborate on that? I'm not very familiar with sed
This seems not to work. And what is first-part, second-part?
The syntax given for -i makes this somewhat specific to GNU sed (one could work with BSD sed, as on MacOS, by passing an argument for backup filename extension).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.