Use xmlstarlet inside an if loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use xmlstarlet inside an if loop
# 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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

2. AIX

Need help on for loop inside ssh

Hi, I am having a file like, #cat file Jun 19 13:08 Jun 19 13:08 Jun 19 13:08 Jun 19 13:14 when I run the below comamnd locally it will work fine, IFS=$'\n'; for i in $(cat file) ;do echo "HI $i" ; done And the output is, HI Jun 19 13:08 HI Jun 19 13:08 HI Jun 19 13:08 HI... (1 Reply)
Discussion started by: sumanthupar
1 Replies

3. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

4. Shell Programming and Scripting

help using EOF inside a for loop

I'm having problems with this example: #!/bin/bash for i in server1 server2 server3 do ssh $i <<EOF >> logfile.txt dat1=`date '+%m/%d/%y'` hst=`hostname` df -Ph | awk -v OFS=',' -v dat="$dat1,$hst" ' {print dat,$1,$6,$3,$4 } ' exit EOF done I'm... (1 Reply)
Discussion started by: kuliksco
1 Replies

5. Shell Programming and Scripting

Ping inside FOR loop

Hello, I'm trying to write (my own primitive) traceroute command that would write out all IPs that packets are hoping through. :) So I've put the ping command inside a for loop #!/bin/bash domain=${1-www.google.com}; #If no argument, then do www.google.com. for ((i=1; i<=30; i++)) #... (9 Replies)
Discussion started by: courteous
9 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Using 'su' inside a loop

Hi, I am using su within a for loop. As you might expect, it prompts for password during each loop execution. Here is my piece of code: for i in $LIST do if then DATABASE=`echo $i | awk -F "|" '{ print $1 }'` USER_ID=`echo $i | awk -F "|" '{ print $2 }'` su - apstage -c... (1 Reply)
Discussion started by: sugan
1 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. Shell Programming and Scripting

Assigning inside for loop

If I have 3 variables and I want to check if any of these is null. If one of them is null then it should be assigned a value of 0.I have the following code below. The output should be 0 is A, 11 is B, 33 is C $a= $b=11 $c=33 $echo $a $b $c $11 33 for i in "${a}" "${b}" "${c}"; do ... (2 Replies)
Discussion started by: thana
2 Replies
Login or Register to Ask a Question