这是000-site.conf的apache2配置文件。 它具有如下配置:
<Proxy balancer://mycluster>
BalancerMember "ajp://10.x.x.xxx:8009" route=node1 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xx:8009" route=node2 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xxx:8009" route=node3 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xx:8009" route=node4 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xx:8009" route=node5 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xx:8009" route=node6 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xxx:8009" route=node7 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
ProxySet lbmethod=byrequests
</Proxy>
我想做的是在#
行之前插入一个ProxySet lbmethod=byrequests
的新BalancerMember。我将使用Shell脚本执行此操作。
因此它应该看起来像:
#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
此外,我还将IP地址用作变量和节点号。
我将编写代码以增加节点数。我已经尝试通过提供行号来使用sed,但是我希望它是动态的而不是静态的。
此解决方案需要最佳实践。请帮助我提供您的想法和最佳做法。
答案 0 :(得分:1)
让我知道这是否对您有帮助。
awk -v s1='"' '
/ProxySet lbmethod=requests/{
print ORS "#BalancerMember " s1 "ajp://10.x.x.xxx:8009" s1 " route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60" ORS $0
next
}
1' Input_file > temp_file && mv temp_file Input_file
因此,根据@ RavinderSingh13的回答,我对其进行了一些修改,并获得了所需的确切解决方案。
#!/bin/bash
your_ip='10.6.0.8'
ip="$your_ip";no="8"; awk -v line="#BalancerMember \"ajp://${ip}:8009\" route=node${no} loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60" '
/ProxySet lbmethod=byrequests/{
print " " line ORS $0
next
}
1' /tmp/000-site.conf > /tmp/000-site.conf.tmp && mv /tmp/000-site.conf.tmp /tmp/000-site.conf
此脚本使用shell变量,并在awk变量中使用它。
答案 1 :(得分:1)
如果您想尝试sed
,请参考以下示例:
sed 's_ProxySet lbmethod=byrequests_#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60\n\t\tProxySet lbmethod=requests_g' 000-site.conf
结果
<Proxy balancer://mycluster>
BalancerMember "ajp://10.x.x.xxx:8009" route=node1 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xx:8009" route=node2 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xxx:8009" route=node3 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xx:8009" route=node4 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xx:8009" route=node5 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xx:8009" route=node6 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
BalancerMember "ajp://10.x.x.xxx:8009" route=node7 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
ProxySet lbmethod=requests
</Proxy>
说明
sed
在文件000-site.conf中用引号's_old-string_new-string_g'
代替。
旧字符串为ProxySet lbmethod=byrequests
它用#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60\n\t\tProxySet lbmethod=requests
代替。 \n\t\t
要求sed引入新行和2个制表符,后跟旧字符串。
写回文件
您可以编写命令以制作000-site.conf的备份文件,并将包含#BalancerMember...
的新文本写入000-site.conf,如下所示:
cp 000-site.conf 000-site.conf.orig
sed 's_ProxySet lbmethod=byrequests_#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60\n\t\tProxySet lbmethod=requests_g' 000-site.conf.orig > 000-site.conf
资源
https://linuxconfig.org/learning-linux-commands-sed
http://www.grymoire.com/Unix/Sed.html
编辑
如果您在外壳中使用sed
命令,则可以执行以下操作:
#!/bin/bash
a=10.0.0.1
no=11
cp 000-site.conf 000-site.conf.orig
sed 's_ProxySet lbmethod=byrequests_#BalancerMember "ajp://'$a':8009" route=node'$no' loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60\n\t\tProxySet lbmethod=requests_g' 000-site.conf.orig > 000-site.conf
答案 2 :(得分:1)
这可能对您有用(GNU sed):
ip='myip://1.2.3.4:1234' n='8'
sed -ri ':a;h;n;/ProxySet/{x;s@(\S.*").*(".*node)[0-9]*@#\1'$ip'\2'$n'@p;x};ba' file
延迟打印每行(第一行除外),并将上一行存储在保留空间(HS)中。如果当前行包含ProxySet
,请修改前面的行,将HS中的行复制为所需结果,然后首先打印。
在替换命令中使用@
作为分隔符,以避免引用/
的情况