我试图在找到名为xyz.txt的文件的某个字符串之后插入一个字符串。
这是xyz.txt文件:
/virtualhosts
{
"alsed-agency-starter.abc.com"
"ecaas-fr-agency-starter.abc.com"
"dvdvd-in-agency-starter.abc.com"
}
我在这里插入了一个新字符串“ geg-de-starter.abc.com”,如下所示:
/virtualhosts
{
"geg-de-starter.abc.com"
"alsed-agency-starter.abc.com"
"ecaas-fr-agency-starter.abc.com"
"dvdvd-in-agency-starter.abc.com"
}
如何使用sed(或任何其他方式)执行此操作?
答案 0 :(得分:0)
awk -v newStr='"geg-de-starter.abc.com"' '
NR==FNR { if ( index($0,newStr) ) gotNew=1; next }
index($0,"/virtualhosts") { gotBeg=1 }
!gotNew && gotBeg && /"/ {
indent = $0
sub(/".*/,"",indent)
print indent newStr
gotBeg = 0
}
1' file file
/virtualhosts
{
"geg-de-starter.abc.com"
"alsed-agency-starter.abc.com"
"ecaas-fr-agency-starter.abc.com"
"dvdvd-in-agency-starter.abc.com"
}