加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 在文件中查找唯一的字符串,在一行中添加并追加?

发布时间:2020-12-14 01:25:50 所属栏目:Linux 来源:网络整理
导读:有人可以帮我解决这个问题吗?我正在寻找一个SED或AWK命令,我可以用它在配置文件( Linux)中找到一个唯一的字符串,上一行并在该行的末尾添加一个字符串? 例如: 配置文件: define hostgroup{ hostgroup_name http-urls ; The name of the hostgroup alias H
有人可以帮我解决这个问题吗?我正在寻找一个SED或AWK命令,我可以用它在配置文件( Linux)中找到一个唯一的字符串,上一行并在该行的末尾添加一个字符串?

例如:

配置文件:

define hostgroup{
hostgroup_name http-urls ; The name of the hostgroup
alias HTTP URLs ; Long name of the group
members domain1.com,domain2.com,domain3.com,
#MyUniqueString
}

在上面的例子中,我想使用SED或AWK来查找#MyUniqeString,上一行以成员开头,并在行尾添加“domain4.com”.

我在下面发现了这个问题,但我需要首先搜索文本文件中的字符串,然后在上面找一行.

Bash script: Appending text at the last character of specific line of a file

有什么建议?

解决方法

试试这个单行:

awk '{a[NR]=$0}/#MyUniqueString/{a[NR-1]=a[NR-1]"domain4.com"}END{for(i=1;i<=NR;i++)print a[i]}' configFile

测试

kent$ cat test.txt 
define hostgroup{
hostgroup_name http-urls ; The name of the hostgroup
alias HTTP URLs ; Long name of the group
members domain1.com,#MyUniqueString
}

kent$ awk '{a[NR]=$0}/#MyUniqueString/{a[NR-1]=a[NR-1]"domain4.com"}END{for(i=1;i<=NR;i++)print a[i]}' test.txt 
define hostgroup{
hostgroup_name http-urls ; The name of the hostgroup
alias HTTP URLs ; Long name of the group
members domain1.com,domain4.com
#MyUniqueString
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读