How can I exctract a revision number from XML in bash?
<?xml version='1.0' encoding='UTF-8'?>
<build>
<actions>
<hudson.model.CauseAction>
<causes>
<hudson.model.Cause_-UserCause>
<authenticationName>roman</authenticationName>
</hudson.model.Cause_-UserCause>
</causes>开发者_JS百科;
</hudson.model.CauseAction>
<hudson.scm.SubversionTagAction>
<build class="build" reference="../../.."/>
<tags class="hudson.util.CopyOnWriteMap$Tree">
<no-comparator/>
<entry>
<hudson.scm.SubversionSCM_-SvnInfo>
<url>http://svn.exoplatform.org/projects/jcr-benchmark/trunk</url>
<revision>70141</revision>
</hudson.scm.SubversionSCM_-SvnInfo>
<list/>
</entry>
<entry>
<hudson.scm.SubversionSCM_-SvnInfo>
<url>https://anonsvn.jboss.org/repos/exo-jcr/core/trunk</url>
<revision>4679</revision>
</hudson.scm.SubversionSCM_-SvnInfo>
<list/>
</entry>
<entry>
<hudson.scm.SubversionSCM_-SvnInfo>
<url>https://anonsvn.jboss.org/repos/exo-jcr/dev-resources/tags/1.2.0</url>
<revision>75</revision>
</hudson.scm.SubversionSCM_-SvnInfo>
<list/>
</entry>
<entry>
<hudson.scm.SubversionSCM_-SvnInfo>
<url>https://anonsvn.jboss.org/repos/exo-jcr/docs-style/tags/1</url>
<revision>2031</revision>
</hudson.scm.SubversionSCM_-SvnInfo>
<list/>
</entry>
<entry>
<hudson.scm.SubversionSCM_-SvnInfo>
<url>https://anonsvn.jboss.org/repos/exo-jcr/jcr/trunk</url>
<revision>4687</revision>
</hudson.scm.SubversionSCM_-SvnInfo>
<list/>
</entry>
<entry>
<hudson.scm.SubversionSCM_-SvnInfo>
<url>https://anonsvn.jboss.org/repos/exo-jcr/junit.framework/tags/1.2.1-GA</url>
<revision>1961</revision>
</hudson.scm.SubversionSCM_-SvnInfo>
<list/>
</entry>
<entry>
<hudson.scm.SubversionSCM_-SvnInfo>
<url>https://anonsvn.jboss.org/repos/exo-jcr/kernel/trunk</url>
<revision>4673</revision>
</hudson.scm.SubversionSCM_-SvnInfo>
<list/>
</entry>
<entry>
<hudson.scm.SubversionSCM_-SvnInfo>
<url>https://*****.*****.org/repos/****/parent/tags/9</url>
<revision>4503</revision>
</hudson.scm.SubversionSCM_-SvnInfo>
<list/>
</entry>
<entry>
<hudson.scm.SubversionSCM_-SvnInfo>
<url>https://*****.*****.org/repos/****/parent/tags/9</url>
<revision>4675</revision>
</hudson.scm.SubversionSCM_-SvnInfo>
<list/>
</entry>
</tags>
</hudson.scm.SubversionTagAction>
<hudson.scm.SVNRevisionState>
<revisions>
<entry>
<string>https://anonsvn.jboss.org/repos/exo-jcr/docs-style/tags/1</string>
<long>2031</long>
</entry>
<entry>
<string>https://anonsvn.jboss.org/repos/exo-jcr/dev-resources/tags/1.2.0</string>
<long>75</long>
</entry>
<entry>
<string>https://anonsvn.jboss.org/repos/exo-jcr/jcr/trunk</string>
<long>4687</long>
</entry>
<entry>
<string>https://anonsvn.jboss.org/repos/exo-jcr/ws/trunk</string>
<long>4675</long>
</entry>
<entry>
<string>https://anonsvn.jboss.org/repos/exo-jcr/kernel/trunk</string>
<long>4673</long>
</entry>
<entry>
<string>https://anonsvn.jboss.org/repos/exo-jcr/junit.framework/tags/1.2.1-GA</string>
<long>1961</long>
</entry>
<entry>
<string>https://anonsvn.jboss.org/repos/exo-jcr/core/trunk</string>
<long>4679</long>
</entry>
<entry>
<string>https://anonsvn.jboss.org/repos/exo-jcr/parent/tags/9</string>
<long>4503</long>
</entry>
<entry>
<string>http://svn.exoplatform.org/projects/jcr-benchmark/trunk</string>
<long>70141</long>
</entry>
</revisions>
</hudson.scm.SVNRevisionState>
<hudson.plugins.chucknorris.RoundhouseAction>
<mStyle>THUMB_UP</mStyle>
<mFact>Chuck Norris can spawn threads that complete before they are started.</mFact>
</hudson.plugins.chucknorris.RoundhouseAction>
<htmlpublisher.HtmlPublisherTarget_-HTMLBuildAction>
<actualHtmlPublisherTarget>
<reportName>Japex Reports</reportName>
<reportDir>TestingResults/</reportDir>
<reportFiles>index.html</reportFiles>
<keepAll>true</keepAll>
<wrapperName>htmlpublisher-wrapper.html</wrapperName>
</actualHtmlPublisherTarget>
<outer-class reference="../actualHtmlPublisherTarget"/>
<build class="build" reference="../../.."/>
<outer-class defined-in="htmlpublisher.HtmlPublisherTarget$HTMLBuildAction" reference="../actualHtmlPublisherTarget"/>
</htmlpublisher.HtmlPublisherTarget_-HTMLBuildAction>
</actions>
<number>38</number>
<result>SUCCESS</result>
<duration>286787</duration>
<charset>UTF-8</charset>
<keepLog>false</keepLog>
<builtOn></builtOn>
<workspace>/home/roman/.hudson/jobs/Express-Testing-JCR-Trunk/workspace</workspace>
<hudsonVersion>1.417</hudsonVersion>
<scm class="hudson.scm.SubversionChangeLogParser"/>
<culprits/>
</build>
I need get revision (4687) from
<entry>
<string>https://anonsvn.jboss.org/repos/exo-jcr/jcr/trunk</string>
<long>4687</long>
</entry>
The xgrep or xpath utilities can help you with this:
xgrep -x '/build/actions/hudson.scm.SubversionTagAction/tags/entry/hudson.scm.SubversionSCM_-SvnInfo[url="https://anonsvn.jboss.org/repos/exo-jcr/jcr/trunk"]/revision/text()' sample.xml
or:
xpath -q -e '/build/actions/hudson.scm.SubversionTagAction/tags/entry/hudson.scm.SubversionSCM_-SvnInfo[url="https://anonsvn.jboss.org/repos/exo-jcr/jcr/trunk"]/revision/text()' sample.xml
Always use an XML parser when dealing with XML content. If you don't you might encounter strange problems, as this classical answer illustrates: RegEx match open tags except XHTML self-contained tags
If the url never change and that <revision> always come after it then you can use the following:
$ grep -A 1 '<url>https://anonsvn.jboss.org/repos/exo-jcr/jcr/trunk' test.xhtml |
tail -1 |
sed -e 's/[^0-9]//g'
精彩评论