使用sed

时间:2018-10-22 18:40:19

标签: regex xml bash sed

我正在尝试在xml标记之间添加hostname。例如:

<?xml version="1.0" encoding="UTF-8"?>
<controller-info>
    <controller-port>{$controller-port}</controller-port>
    <controller-ssl-enabled>false</controller-ssl-enabled>
    <enable-orchestration>false</enable-orchestration>
    <unique-host-id></unique-host-id>
</controller-info>

我希望标签<unique-host-id></unique-host-id>的值是它正在运行的计算机的主机名。因此,在成功执行sed之后,它应该类似于以下内容。

<?xml version="1.0" encoding="UTF-8"?>
<controller-info>
    <controller-port>{$controller-port}</controller-port>
    <controller-ssl-enabled>false</controller-ssl-enabled>
    <enable-orchestration>false</enable-orchestration>
    <unique-host-id>MISDRFI-J0CQTR2</unique-host-id>
</controller-info>

现在我已经尝试运行此程序,但是它根本不起作用,请问有什么解决方案吗?

sed -i -e "s/\<\/unique-host-id\>/$hostname\<\/unique-host-id\>/g" sample.xml

1 个答案:

答案 0 :(得分:-1)

我找到了答案,看来sed -i -e "s/<\/unique-host-id>/$(hostname)<\/unique-host-id>/g" sample.xml命令需要更正

const newArray = oldArray.map((x, i) => ({ // If the object is dynamic you can spread it out here and add the ID ...x, // Use the items index in the array as a unique key uniqueId: i, }));

这很容易将值添加到标签中。