【发布时间】:2021-04-24 15:12:45
【问题描述】:
在我的 Linux 培训中,我有一个练习要求替换 config.xml 文件中的语句。
我需要替换语句:
<app-connector port="${APP_CONNECTOR_PORT}" address="192.168.0.254"
通过这个:
<app-connector port="${APP_CONNECTOR_PORT}" address="0.0.0.0"
我已经开始grepconfig.xml 文件中的所有现有语句,但它不起作用:
Cat config.xml | grep '<app-connector port="${APP_SYSTEM_CONNECTOR_PORT}" address="192.168.0.254"'
还有
Cat config.xml | grep -F '<app-connector port="${APP_SYSTEM_CONNECTOR_PORT}" address="192.168.0.254"'
你能告诉我如何在所有config.xml文件中找到这个语句吗?
如何替换为:
<app-connector port="${APP_CONNECTOR_PORT}" address="0.0.0.0"
【问题讨论】:
-
使用 XML 特定的命令行工具,例如 xmlstarlet,因为 sed , awk, grep, and so on leads to undesired results。例如,尝试以下 xmlstarlet 命令:
xml ed -L -u "//app-connector[@port='\${APP_CONNECTOR_PORT}']/@address[. = '192.168.0.254']" -v "0.0.0.0" /some/path/to/config.xml- 此命令替换192.168.0.254的所有address属性值,如果它们关联的 xml 节点是<app-connector>并且它也有一个port${APP_CONNECTOR_PORT}的属性值。
标签: xml bash sed grep config-files