Find the only string name based on the sub string name in a file.
Command used:
cat test.xml | grep -i current | awk -F / '{print $NF}' | tr -d
What works:
File content:
<Context path="/SomeApi" reloadable="false" docBase="http://some-domain/CURRENT/SomeApi-CURRENT.war"
Output (desired output):
SomeApi-CURRENT.war
What does not work:
File content:
<Context path="/SomeApi" reloadable="false" docBase="http://some-domain/CURRENT/SomeApi-CURRENT.war" workDir="/U01/someApi/work
Output:
work className=URLStandardContext unpackWAR=true>
Need output SomeApi-CURRENT.war
Advertisement
Answer
It appears, you are interested in the docBase attribute of your xml elements. Specifically those where the value string contains “current” as a substring.
grep -i current test.xml | grep -o " docBase="[^"]*" | rev | cut -d'/' -f1 | rev
But this is a somewhat crude solution. Multiple assumptions have to be made in order to guarantee correctness.
As using grep etc. is not ideal, you would probably want to check xmllint which is a command line tool for parsing xml inputs. With XPath the command could look something like this:
xmllint --xpath "//Context[contains(translate(@docBase,'CENRTU', 'cenrtu'), 'current')]/@docBase" test.xml | rev | cut -d'/' -f1 | cut -c2- | rev