2

I have a file 'EXXARS - Physical Inventory Adjustment Report US PDF_es_ES.xlf'

file data:

<header>
 <prop-group name="ora_reconstruction">
    <prop prop-type="TemplateCode">XXPO_PHYS_INV_ADJ_ES_AS</prop>
    <prop prop-type="extractorVersion">10.1.3.4.2_1.5</prop>
 </prop-group>

I need extract the value between and result: I need this value in the variable XXPO_PHYS_INV_ADJ_ES_ES.

I have coomand which is not working

filename='ES - Physical Inventory Adjustment Report US PDF_es_ES.xlf'
sed -n 's:.*<prop prop-type="TemplateCode">\(.*\)</prop>.*:\1:p' filename > ${LOBCODE}
echo " --> ${LOBCODE}"
2
  • I'm assuming there's more to the .xlf file than just what you have there, correct? Otherwise, it's malformed XML and the tag isn't really appropriate.
    – ErikF
    Mar 2, 2018 at 7:27
  • yes,,,I gave just frist 10 lines
    – Arun GoWdA
    Mar 2, 2018 at 7:34

3 Answers 3

2

Assuming that the XML file is well formed:

<?xml version="1.0"?>
<header>
  <prop-group name="ora_reconstruction">
    <prop prop-type="TemplateCode">XXPO_PHYS_INV_ADJ_ES_AS</prop>
    <prop prop-type="extractorVersion">10.1.3.4.2_1.5</prop>
  </prop-group>
</header>

Using XMLStarlet:

xmlfile='ES - Physical Inventory Adjustment Report US PDF_es_ES.xlf'
LOBCODE=$( xml sel -t -v '//prop[@prop-type="TemplateCode"]' "$xmlfile" )

This uses XMLStarlet to extract the value of the prop node whose prop-type attribute is TemplateCode. The variable LOBCODE will get the value XXPO_PHYS_INV_ADJ_ES_AS with the above XML.

XMLStarlet may sometimes be installed as xmlstarlet rather than as xml.


There are two issues with your code:

  1. You redirect the output of the sed command to a file whose name is given by $LOBCODE. If $LOBCODE is empty, then this redirection will fail. What I assume you wanted to do was to assign the output of sed to the variable LOBCODE. This is done with a command substitution as I've shown above. Note that sed is a poor choice of tool for parsing XML data.

  2. You instruct sed to work with a file called filename. To have sed work on the value of the variable filename you will need to use $ in front of the variable name. Also, since the filename has spaces in it, you need to double quote the variable expansion (you should always do this, regardless). Hence you should use "$filename" (I've used a more descriptive variable name in my code above).

0
0

@Kusalananda's answer is excellent, but if you have xmllint instead, you can use this:

filename='ES - Physical Inventory Adjustment Report US PDF_es_ES.xlf'
LOBCODE=$(xmllint --xpath "header/prop-group[@name='ora_reconstruction']/prop[@prop-type='TemplateCode']/text()" "$filename")

Notes:

  • I used the full path specification in case you only want the prop-group with the "ora-reconstruction" attribute. @Kusalananda's method assumes you want any prop element with an attribute of "TemplateCode": which method is preferable depends on your data.
  • text() returns just the text inside the prop element (otherwise, xmllint returns the surrounding element as well.)

Example run of the important part:

$ xmllint --xpath "header/prop-group[@name='ora_reconstruction']/prop[@proptype='TemplateCode']/text()" "ES - Physical Inventory Adjustment Report US PDF_es_ES.xlf"
XXPO_PHYS_INV_ADJ_ES_AS
1
  • we cannot install the upgraded switch of xmllint and XMLStarlet. due to limitation. can you please to extract using the string? Oracle suggests the same
    – Arun GoWdA
    Mar 12, 2018 at 11:49
-2
$ cat test.xml
<header>
 <prop-group name="ora_reconstruction">
    <prop prop-type="TemplateCode">XXPO_PHYS_INV_ADJ_ES_AS</prop>
    <prop prop-type="extractorVersion">10.1.3.4.2_1.5</prop>
 </prop-group>

$ awk -F"[<>]" '/TemplateCode/{print $3;exit}' test.xml
XXPO_PHYS_INV_ADJ_ES_AS
2
  • Using awk and friends for XML processing is almost always a bad idea, because they don't know anything about the structure of the data and are incredibly brittle. For example, if "TemplateCode" shows up in some other (non-prop related) context in the XML file before the one you're interested in, you'll end up with erroneous results. There are enough common tools that know about XML nowadays!
    – ErikF
    Mar 2, 2018 at 8:32
  • we cannot install the upgraded switch of xmllint and XMLStarlet. due to limitation. can you please to extract using the string? Oracle suggests the same
    – Arun GoWdA
    Mar 12, 2018 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .