I have an xml file like
我有一个像xml这样的文件
<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
我想使用shell脚本添加一个新的changeSet标记,如下所示
<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.
有没有简单的方法在shell脚本中执行此操作?我还要在标签中更改id和sql文件名。
2 个解决方案
#1
1
Quick, dirty, unreliable. Does not parse XML - this just assumes that your input file will always retain the current line formatting.
快速,肮脏,不可靠。不解析XML - 这只是假设您的输入文件将始终保留当前行格式。
cat test.xml
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
确定最后一个changeSet标记的行号
line_no="`grep -n '</changeSet>$' test.xml | cut -f1 -d: | tail -n 1`" ; echo "$line_no"
Add new changeSet
添加新的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
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>
#2
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.
它不是一个格式良好的XML文件,但是如果你只想附加到文件的末尾,那么使用echo或cat命令并使用>> operator将其输出发送到文件的末尾。
#1
1
Quick, dirty, unreliable. Does not parse XML - this just assumes that your input file will always retain the current line formatting.
快速,肮脏,不可靠。不解析XML - 这只是假设您的输入文件将始终保留当前行格式。
cat test.xml
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
确定最后一个changeSet标记的行号
line_no="`grep -n '</changeSet>$' test.xml | cut -f1 -d: | tail -n 1`" ; echo "$line_no"
Add new changeSet
添加新的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
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>
#2
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.
它不是一个格式良好的XML文件,但是如果你只想附加到文件的末尾,那么使用echo或cat命令并使用>> operator将其输出发送到文件的末尾。