如何在xml文件中添加行

如何在xml文件中添加行

我想在 xml 文件中添加一行。我的xml文件包含如下:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration>     
  <ftp_destination_ip>0.0.0.0</ftp_destination_ip>
  <ftp_destination_user>root</ftp_destination_user>
  <ftp_destination_pass>undefined</ftp_destination_pass>        
  <other_unix_backup_path>/root/BACKUP</other_unix_backup_path>
  <other_backup_folder>BACKUP</other_backup_folder>
  <source_confirmation_needed>false</source_confirmation_needed>
  <keep_ems_running_after_restore>false</keep_ems_running_after_restore>
  <remote_connection_timeout>120000</remote_connection_timeout >
    <history>
        <manage_backup_history>true</manage_backup_history>
        <backup_history_num_of_days>7</backup_history_num_of_days>
    </history>
</configuration>

我想添加两行,即 XML 中的第三行和第四行:

  <remote_connection_protocol>sftp</remote_connection_protocol>
  <ftp_destination_ip>0.0.0.0</ftp_destination_ip>

输出应该是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration>     
  <!--remote connection protocol>false|ftp|sftp</remote connection protocol-->
  <remote_connection_protocol>sftp</remote_connection_protocol>
  <ftp_destination_ip>0.0.0.0</ftp_destination_ip>
  <ftp_destination_user>root</ftp_destination_user>
  <ftp_destination_pass>undefined</ftp_destination_pass>        
  <other_unix_backup_path>/root/BACKUP</other_unix_backup_path>
  <other_backup_folder>BACKUP</other_backup_folder>
  <source_confirmation_needed>false</source_confirmation_needed>
  <keep_ems_running_after_restore>false</keep_ems_running_after_restore>
  <remote_connection_timeout>120000</remote_connection_timeout >
    <history>
        <manage_backup_history>true</manage_backup_history>
        <backup_history_num_of_days>7</backup_history_num_of_days>
    </history>
</configuration>

谢谢

答案1

尝试这个,

sed '4i\  <remote_connection_protocol>sftp</remote_connection_protocol>\n  <ftp_destination_ip>0.0.0.0</ftp_destination_ip>' file.xml
  • 4i将内容添加到文件的第 4 行。
  • \n换行符。

如果有效,请添加-i内联编辑选项。

相关内容