使用sed或awk附加重复文本?(Append repeated text using sed or awk?)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
使用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; } );

我怎么能用sedawk这样的传统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}' file

This 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

更多推荐

本文发布于:2023-04-12 20:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4758d67a6f0ab6c6d92e8b52f29b3203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   awk   sed   text   repeated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!