1

我有以下配置文件:

servers = (
  {
    host = "localhost";
    ...
    timeout = 5;
  },
  {
    host = "127.0.0.1";
    ...
    timeout = 0;
  },
  {
    host = "example.com";
    ...
    timeout = 99;
  }  
);

我想在每个部分的末尾附加“索引”设置,因此配置如下所示:

servers = (
  {
    host = "localhost";
    ...
    timeout = 5;
    index = 1;
  },
  {
    host = "127.0.0.1";
    ...
    timeout = 0;
    index = 2;
  },
  {
    host = "example.com";
    ...
    timeout = 99;
    index = 3;
  }  
);

如何使用sedawk等传统的 Unix 工具来做到这一点?

4

1 回答 1

4

这会index = ...在每行timeout作为第一个单词之后添加一行。

awk '1;$1=="timeout"{printf "    index = %d;\n", ++i}' file
于 2013-08-24T09:36:41.273 回答