Home > database >  read multiple name value pair from xml tag
read multiple name value pair from xml tag

Time:12-07

I have an xml tag where it has multiple name and value pairs

here is my xml tag from my file

<AsyncLogger name="org.mule.service.http" level="WARN"/>

using below command to read

  1. AsyncLogger name and its value
  2. log level and its value
cat log4j2.xml | perl -e 'while (<>) { next if (/<!--.*-->/);if (/<!--/) { while (<>) {last if (/-->/) }}else {print "$_"; }} ' | xmllint --format -| grep org.mule.service.http|awk -F'=' '{print $2,$4}'

this is what it is printing

"org.mule.service.http" level

Expecting output with comma separated

org.mule.service.http,WARN

CodePudding user response:

Using xpath with concat() and string() functions:

$ xpath -q -e 'concat(string(//AsyncLogger/@name), ",", string(//AsyncLogger/@level))' file.xml
org.mule.service.http,WARN

CodePudding user response:

Like this:

$ xmlstarlet sel -t -v 'concat(//AsyncLogger/@name, ",", //AsyncLogger/@level)' file.xml
org.mule.service.http,WARN

$ xmllint --xpath 'concat(//AsyncLogger/@name, ",", //AsyncLogger/@level)' /tmp/file.xml
org.mule.service.http,WARN 
  • Related