使用脚本修改文件结构

时间:2017-06-22 13:53:30

标签: python shell file

我有一个文件说f1,格式如下:

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", ];
  ...........
  }

我需要对其进行修改,使每个列表都颠倒过来,并且","应该删除列表末尾。例如,输出格式应为:

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

如何解决此问题?我必须使用任何脚本吗?我对shell脚本不太熟悉。

1 个答案:

答案 0 :(得分:0)

理想情况下,您可以使用能够识别Dot语法的内容,从而可以可靠地查找和修复无效语法。您可以尝试Dot plugin for IDEA(使用 F2 移动到文件中的下一个问题)。对于非常简单(但有风险,因为它不具有语法感知)的替换,您可以使用sed

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

至于sed的反转,一般模式是:

sed -i 's/\(first-part\)\(second-part\)/\2\1/' your-file