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

4
  • Thanks! I edited Jun 18, 2019 at 13:00
  • It should be 3th and 4th line. My mistake. fixed. Jun 18, 2019 at 13:01
  • but literally its 4th and 5th line
    – Siva
    Jun 18, 2019 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, 2019 at 13:26

1 Answer 1

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
  • 1
    My mistake. It works! Thanks a lot! Jun 19, 2019 at 5:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .