Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Use xmlstarlet inside an if loop

Tags
shell script, shell scripts, unix, xmlstarlet

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 06-07-2017
Lightbulb Use xmlstarlet inside an if loop

I have a XML file of little huge size. I have to build a logic to get the count of the tag
Code:
<capacity>

.

And have an if loop such that all the <capacity> blocks are captured one after the other.

sample input file - sample1.xml

Code:
<subcolumns><capacity><name>45.90</name>
<index>0</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
<capacity><name>57.09</name>
<index>1</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
<capacity><name>55</name>
<index>2</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
</subcolumns>

Please find the below snippet for achieving my goal

Code:
#!/bin/bash
set -x
sensor_count=`grep -c "<capacity>" sample1.xml`
for ((i=1;i<=$sensor_count;i++));
do
        xmlstarlet sel -t -c '//capacity["$i"]' -n sample1.xml
done

Now the problem is when I use the $i variable along the xmlstarlet command it is printing the entire file rather than
only the occurence.
# 2  
Old 06-07-2017
I don't know about the xmlstarlet syntax, but at any rate, the use of single quotes is preventing the variable i from being expanded.

So try changing :
Code:
'//capacity["$i"]'

to
Code:
"//capacity[\"$i\"]"

👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Write a while loop inside for loop? xxhieixx UNIX for Dummies Questions & Answers 3 04-16-2015 11:16 AM
help using EOF inside a for loop kuliksco Shell Programming and Scripting 1 10-30-2012 12:53 AM
AWK inside For loop thulasidharan2k Shell Programming and Scripting 3 04-20-2011 12:58 PM
xmlstarlet parse non en_US characters unclecameron Shell Programming and Scripting 15 12-21-2010 01:36 PM
BASH loop inside a loop question rethink Shell Programming and Scripting 4 09-15-2010 07:58 AM
Need help using sed inside the loop. dazdseg Shell Programming and Scripting 3 06-04-2010 01:00 AM
xmlstarlet parse field from file unclecameron Shell Programming and Scripting 1 05-20-2010 08:18 PM
SED inside while loop paoie UNIX for Dummies Questions & Answers 5 11-17-2009 05:07 AM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 04:59 PM
variable inside variable inside loop headache hcclnoodles Shell Programming and Scripting 6 06-18-2009 04:55 PM


All times are GMT -4. The time now is 05:14 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password