使用sed或awk附加重复文本?

时间:2013-08-24 09:31:26

标签: text sed awk transformation

我有以下配置文件:

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

我想在每个部分的末尾附加“index”设置,所以配置如下所示:

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

如何使用 sed awk 等传统Unix工具执行此操作?

1 个答案:

答案 0 :(得分:4)

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

awk '1;$1=="timeout"{printf "    index = %d;\n", ++i}' file