0

I have an xml file like

<changeSet id="1" author="naveen" dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/IntialVersion.sql" />
    </changeSet>
    <changeSet id="2" author="naveen"  dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/FirstRev.sql" />
    </changeSet>
    <changeSet id="3" author="naveen"  dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/23_12_2011.sql" />
    </changeSet>

I want to add a new changeSet tag like below using shell scripting

 <changeSet id="4" author="naveen"  dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/24_12_2011.sql" />
    </changeSet>

Is there any simple way to do this in shell scripting? I have to change id and sql file name also in the tag.

1
  • 2
    echo '<newtag />' >> file.xml. It won't be valid, but it's a new tag in the xml file... Commented Jan 2, 2012 at 16:12

2 Answers 2

1

Quick, dirty, unreliable. Does not parse XML - this just assumes that your input file will always retain the current line formatting.

cat test.xml

<intro>
a\b&c;
d$e
</intro>
<changeSet id="1" author="naveen" dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/IntialVersion.sql" />
    </changeSet>
    <changeSet id="2" author="naveen"  dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/FirstRev.sql" />
    </changeSet>
    <changeSet id="3" author="naveen"  dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/23_12_2011.sql" />
    </changeSet>
<outro>
</outro>

Determine the line number of last changeSet tag

line_no="`grep -n '</changeSet>$' test.xml | cut -f1 -d: | tail -n 1`" ; echo "$line_no"

Add new changeSet

[[ -n "$line_no" ]] && sed "$line_no"'s#$#\n<changeSet id="4" author="naveen"  dbms="oracle">\n  <sqlFile path="/appl/Liquibase/sql/24_12_2011.sql" />\n</changeSet>#' test.xml > test.xml.new

cat test.xml.new

<intro>
a\b&c;
d$e
</intro>
<changeSet id="1" author="naveen" dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/IntialVersion.sql" />
    </changeSet>
    <changeSet id="2" author="naveen"  dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/FirstRev.sql" />
    </changeSet>
    <changeSet id="3" author="naveen"  dbms="oracle">
      <sqlFile path="/appl/Liquibase/sql/23_12_2011.sql" />
    </changeSet>
<changeSet id="4" author="naveen"  dbms="oracle">
  <sqlFile path="/appl/Liquibase/sql/24_12_2011.sql" />
</changeSet>
<outro>
</outro>
Sign up to request clarification or add additional context in comments.

Comments

0

It's not really a well-formed XML file, but if you just want to append to the end of the file, then use echo or cat command and send its output to the end of the file with >> operator.

2 Comments

I dont want to add it in the last. It is not full xml. It is a part of my full xml file. I want to add this tag right after the previous tag in between the xml file with the use of shell script. any help??
Can you show your full xml structure? It will be easier to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.