0

I am having the following xml,

<?xml version="1.0" encoding="UTF-8"?> <power-domains> <power-domain name="Security" cache-type="default"> <authentication> <login-module code="test.module" flag="required" module="com.test.nms"> <module-option name="principal" value="admin"/> <module-option name="userName" value="admin"/> <module-option name="password" value="YSFSDwe"/> </login-module> </authentication> </power-domain> </power-domains> 

By using shell script, I need parse the above content and get value of userName and password?

Please guide me how to do?

    1 Answer 1

    2

    You can use this command to extract the values:

    xmlstarlet sel -t \ -v '//module-option[@name = "userName"]/@value' \ -nl \ -v '//module-option[@name = "password"]/@value' \ file.xml 

    and, assuming your shell is bash, to read those values into shell variables, use a Process Substitution:

    { IFS= read -r userName IFS= read -r password } < <( xmlstarlet sel -t -v '//module-option[@name = "userName"]/@value' -nl -v '//module-option[@name = "password"]/@value' file.xml ) echo "$userName"; echo "$password" 
    admin YSFSDwe 
    1
    • Thanks. Works fine with xmlstartlet. Is there any other way without using xmlstarlet? Since the package is not available by default in CentOS/RHEL.
      – Picasu27
      CommentedJan 20, 2020 at 7:25

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.