使用sed或awk附加重复文本?(Append repeated text using sed or awk?)
我有以下配置文件:
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工具做到这一点?
I have the following config file:
servers = ( { host = "localhost"; ... timeout = 5; }, { host = "127.0.0.1"; ... timeout = 0; }, { host = "example.com"; ... timeout = 99; } );I want to append "index" setting at the end of each section so the config look like this:
servers = ( { host = "localhost"; ... timeout = 5; index = 1; }, { host = "127.0.0.1"; ... timeout = 0; index = 2; }, { host = "example.com"; ... timeout = 99; index = 3; } );How can I do this with conventional Unix tools like sed or awk?
最满意答案
这会在每行以timeout作为第一个单词后添加一行index = ...
awk '1;$1=="timeout"{printf " index = %d;\n", ++i}' fileThis adds a line with index = ... after each line that has timeout as the first word.
awk '1;$1=="timeout"{printf " index = %d;\n", ++i}' file更多推荐
发布评论