1

I am extracting an attribute value from xml file, but I get an error. I'd like to extract the value for key="qua" in the firstpart element. Here is my script, but below you find the errors:

#!/bin/bash

myfile=$1

myvar=$(echo 'cat //firstpart/step/category/id/info[@key="qua"]/@value' | xmllint --xpath "$myfile" | awk -F'[="]' '!/>/{print $(NF-1)}')

echo "$myvar"

how my xml file looks like:

<?xml version='1.0' encoding='UTF-8'?>
<firstpart>
    <step name="Home">    
        <category name="one">
            <id name="tools">
                <info key="qua" value="1"/>        
            </id>
        </category>
    </step>
    <step name="Contact">    
        <category name="two">
            <id name="tools">
                <info key="qua" value="2"/>        
            </id>
        </category>
    </step>
    ...
</firstpart>
<secondpart>
    <step name="office">    
        <category name="one">
            <id name="tools">
                <info key="qua" value="100"/>        
            </id>
        </category>
    </step>
    <step name="Contact">    
        <category name="two">
            <id name="tools">
                <info key="qua" value="200"/>        
            </id>
        </category>
    </step>
    ...
</secondpart>

the errors I get:

awk: run time error: negative field index $-1
    FILENAME="-" FNR=71 NR=71

./mybash.sh: line 3: $: command not found
./mybash.sh: line 4: $: command not found
7
  • Have you tried saving the xmllint output to a file and running awk on that? I assume not since your problem will be extremely obvious if you do. Commented May 9, 2017 at 13:08
  • @EdMorton how can i do it? could you provide an answer please? Commented May 9, 2017 at 13:09
  • Instead of piping to awk, redirect to a file. Then run awk on that file and get the same error. Then inspect the file to see the problem. Commented May 9, 2017 at 13:16
  • 1
    Your file is not valid XML. Commented May 9, 2017 at 13:26
  • @Cyrus why is it not a valid one? i have excluded the tail of the file and included three dots in the middle to say that they are other stuff there... Commented May 9, 2017 at 13:29

2 Answers 2

4

It looks like you call xmllint in wrong way.

xmllint --xpath '//firstpart/step/category/id/info[@key="qua"]/@value' FILE.xml

Result:

value="1" value="2"

Complete script:

#!/bin/bash

str=$(xmllint --xpath '//firstpart/step/category/id/info[@key="qua"]/@value' $1)

entries=($(echo ${str}))
for entry in "${entries[@]}"; do
    result=$(echo $entry | awk -F'[="]' '!/>/{print $(NF-1)}')
    echo "result: $result"
done

May be, it's not better solution, but at least it works :)

Sign up to request clarification or add additional context in comments.

1 Comment

how can i get the value only and not the ` value="1"` ? on the the number
3

Get value of attribute with xmllint:

xmllint --xpath 'string(//firstpart/step[1]/category/id/info/@value)' file.xml

Output:

1

1 Comment

I like this solution but since Bor laze was first to answer, so I accept that one as the answer to the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.