0

I want to add one line in xml file. My xml file contains as follows:

<?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>

And I want to add two lines that will be 3th and 4th line in the XML:

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

The output should be like that:

<?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>

Thanks

  • Thanks! I edited – Shalev Sason Jun 18 at 13:00
  • It should be 3th and 4th line. My mistake. fixed. – Shalev Sason Jun 18 at 13:01
  • but literally its 4th and 5th line – msp9011 Jun 18 at 13:03
  • while sed, awk and friends could do the job, you might whish to use xmlstartlet to do such thing, see p16 Include one XML document into another using XInclude xmlstar.sourceforge.net/doc/xmlstarlet.pdf – Archemar Jun 18 at 13:26
2

Try this,

sed '4i\  <remote_connection_protocol>sftp</remote_connection_protocol>\n  <ftp_destination_ip>0.0.0.0</ftp_destination_ip>' file.xml
  • 4i to add the content at the 4th line of the file.
  • \n for a newline.

if it works, add -i option for inline edit.

  • 1
    My mistake. It works! Thanks a lot! – Shalev Sason Jun 19 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.