I have a requirement where I need to add a new entry for the custom top to be created, in the context_file of ebs application installed in Oracle Linux 6. The context_file is an XML file. We need to search for a string and then we need to add a new entry just after the searched string.

  1. Search the string AU_TOP> in the file.
  2. Insert a string in a new line just after AU_TOP>

    <TEST_TOP oa_var="s_testtop" oa_type="PROD_TOP" oa_enabled="FALSE">/u01/oracle/oracle/apps/apps_st/appl/test/12.0.0</TEST_TOP>

  3. Save the file.

How can this be accomplished using a shell script?

  • 1
    With great difficulty unless you include sample input and output. And even then, parsing XML with shell scripting is like performing brain surgery with a plastic teaspoon. – jasonwryan Mar 31 '17 at 4:32
  • sed '/AU_TOP>/a /u01/oracle/oracle/apps/apps_st/appl/test/12.0.0'? – heemayl Mar 31 '17 at 4:33
  • Thanks for your comment jason. I ran the command it inserted the the line but how to save the file with the changes? – user3436389 Apr 2 '17 at 8:40
  • Eg - My xml file contains the following entry <AU_TOP oa_var="s_autop" oa_type="PROD_TOP" oa_enabled="FALSE">/u01/oracle/VIS/apps/apps_st/appl/au/12.0.0</AU_TOP> <FND_TOP oa_var="s_fndtop" oa_type="PROD_TOP" oa_enabled="TRUE">/u01/oracle/VIS/apps/apps_st/appl/fnd/12.0.0</FND_TOP> – user3436389 Apr 2 '17 at 8:41
  • When I gave the following command sed '/AU_TOP>/a /u01/oracle/oracle/apps/apps_st/appl/test/12.0.0' text.txt it inserted a newline in the output but how to save the new line inserted in the file? – user3436389 Apr 2 '17 at 8:42

Stealing heemayl's idea from the comments, then adding -i for in-place editing and newlines for the 'append' command and desired newline afterwards:

sed -i '/AU_TOP>/a\
<TEST_TOP oa_var="s_testtop" oa_type="PROD_TOP" oa_enabled="FALSE">/u01/oracle/oracle/apps/apps_st/appl/test/12.0.0</TEST_TOP>

' context_file.xml
  • Hi Jeff, I tried what you said and it worked however I am facing another issue, the sed command is not taking any $ variables, Eg #!/bin/bash export CONTEXT_FILE=test_dba.xml export APPL_TOP=/root echo -n "Name: " read name top=$(echo "<${name^^}_TOP oa_var=\"s_echo $nametop\" oa_type=\"PROD_TOP\" oa_enabled=\"FALSE\">echo $APPL_TOP/$name/12.0.0</${name^^}_TOP>") echo $top sed -i '/AU_TOP>/a "${top}"' $CONTEXT_FILE however when passing the value for a variable like ERP it only appends $top as a value in the xml file and does not pass the value stored in the $top. – user3436389 Apr 10 '17 at 18:10
  • Happy to hear it's working! To get variables' values into sed, you have to use double quotes; single quotes prevent the shell from inserting variable values. – Jeff Schaller Apr 10 '17 at 18:38

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.