Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Tags
shell script, unix, xmlstarlet

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-07-2017   -   Original Discussion by ramprabhum
ramprabhum's Unix or Linux Image
ramprabhum ramprabhum is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 3 July 2017, 10:36 AM EDT
Posts: 15
Thanks: 10
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old Unix and Linux 06-07-2017   -   Original Discussion by ramprabhum
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 April 2018, 3:57 PM EDT
Location: Amsterdam
Posts: 11,781
Thanks: 542
Thanked 3,430 Times in 3,024 Posts
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\"]"

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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
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
xmlstarlet parse field from file unclecameron Shell Programming and Scripting 1 05-20-2010 08:18 PM
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



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