例如:
配置文件:
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]=kent$ cat test.txt define hostgroup{hostgroup_name http-urls ; The name of the hostgroupalias http URLs ; Long name of the groupmembers domain1.com,#MyUniqueString}kent$ awk '{a[NR]=}/#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 hostgroupalias http URLs ; Long name of the groupmembers domain1.com,domain4.com#MyUniqueString}}/#MyUniqueString/{a[NR-1]=a[NR-1]"domain4.com"}END{for(i=1;i<=NR;i++)print a[i]}' configfile
测试
总结以上是内存溢出为你收集整理的linux – 在文件中查找唯一的字符串,在一行中添加并追加?全部内容,希望文章能够帮你解决linux – 在文件中查找唯一的字符串,在一行中添加并追加?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)