Search code examples
xmlunixterminalxmlstarlet

xml parsing of one value


I have a file that keeps appending the following xml. What I want to get from into a variable is the percentComplete from the jobStatus. How would I get that? Not sure how to parse the xml. I tried using XMLStarlet but failed. I want to get the percentComplete in a variable that I can use in the shell.

<batchStatus name="" submissionTime="1/23/12 10:00:26 AM" sentBy="mike" 
timeElapsed="43 second(s)" timeRemaining="4 minute(s)" timeElapsedSeconds="43"
timeRemainingSeconds="294" percentComplete="12" resumePercentComplete="0" 
status="Processing" batchid="FD66DC21-6AA4-47FB-A3F0-7300C7BDAB8A" /batchStatus>
<jobStatus name="file.mov" submissionTime="1/23/12 10:00:26 AM" sentBy="mike"
 jobType="Compressor" priority="HighPriority" timeElapsed="43 second(s)" 
timeRemaining="4 minute(s)" timeElapsedSeconds="43" timeRemainingSeconds="294"
 percentComplete="12" resumePercentComplete="0" status="Processing: Transcoding" 
jobid="FDF1A488-51B9-4B9A-908B-FD5D95CE7E92" 
batchid="FD66DC21-6AA4-47FB-A3F0-7300C7BDAB8A" /jobStatus>

Solution

  • You can try something like this -

    var=$(awk -v FS="[ =\"]" '/^<jobStatus/,/\/jobStatus>$/{for (i=1;i<=NF;i++) if ($i~/percentComplete/) print $(i+2)}' file | tail -n 1)
    [jaypal:~/Temp] echo $var
    12