XMLファイルに行を追加する方法

XMLファイルに行を追加する方法

XMLファイルに1行を追加したいです。私の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に3行目と4行目の2行を追加したいと思います。

  <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インライン編集オプションを追加してください。

おすすめ記事