string - 如何删除 awk 中的空格和换行符以进行字符串插入?
问题描述
所以,我有这样的 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>
我想要做的是在行之前插入一个带有 # 的新 BalancerMember ProxySet lbmethod=byrequests
。我将使用 shell 脚本来执行此操作。
所以它应该看起来像: #BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
我尝试过的是:
awk -v s1='"' -v ip="10.0.7.1" -v no="8" '
/ProxySet lbmethod=byrequests/{
print "\n\t\t#BalancerMember " s1 "ajp://" ip ":8009" s1 " route=node" no " loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60"
next
}
1' /tmp/000-site.conf > /tmp/000-site.conf.tmp && mv /tmp/000-site.conf.tmp /tmp/000-site.conf
}
因此,上述解决方案可以正常工作,但会留下我不想要的换行符。我试过删除 ORS。
解决方案
您能否尝试以下操作。(创建其中包含新行值的line
变量)awk
awk -v line='#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60' '
/ProxySet lbmethod=byrequests/{
print " " line ORS $0
next
}
1' Input_file
说明:在此处添加对上述代码的说明。
awk -v line='#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60' ' ##Creating variable line.
/ProxySet lbmethod=byrequests/{ ##Checking condition here if a line has string ProxySet lbmethod=byrequests then do following.
print " " line ORS $0 ##Printing space then variable line ORS and then print currrent line value then.
next ##Mentioning next out of the box keyword to skip all further statements.
}
1 ##Mentioning 1 will print the lines here, awk works on condition then action, making condition true here, print action will happen.
' Input_file ##Mentioning Input_file name here.
推荐阅读
- runtime - 多项式时间内的实重量背包
- java - 如何修复 ScrollView 内的 ConstraintLayout 不滑动?
- python-3.x - 如何更新权重和偏差
- arrays - 合并数组时继承自定义方法
- ios - 线程 1:在 swift 中发送信号 SIGABRT
- python - 除了 OSX 上现有的 pyenv 安装之外,如何安装 Anaconda?
- java - 数组索引在 for 内停止正常工作
- azure-devops - Nuget Pack .nuspec 文件 Azure Pipeline 不获取 csproj 程序集属性。但是同一个项目可以在本地打包
- php - 如何在 BuddyPress 中以编程方式将用户设置为垃圾邮件?
- ajax - 如何拆分 XMLHttpRequest 中的值?