1

I have created shell script to read a file and store elements into array.

Now i want to remove some elements from array which has </ in it.

my xml file:

<Execution_Stats>
<Total_Executions>1</Total_Executions>  
<Total_Errors>0</Total_Errors>
<SQL_Analysis>
    <NOT_COMPARED>1</NOT_COMPARED>
    <MATCHED>0</MATCHED>
    <NOT_MATCHED>0</NOT_MATCHED>
    <ERROR>0</ERROR>
</SQL_Analysis>
<Data_Analysis>
    <NOT_COMPARED>0</NOT_COMPARED>
    <MATCHED>0</MATCHED>
    <NOT_MATCHED>1</NOT_MATCHED>
    <ERROR>0</ERROR>
</Data_Analysis>
<Graph_Analysis>
    <NOT_COMPARED>1</NOT_COMPARED>
    <MATCHED>0</MATCHED>
    <NOT_MATCHED>0</NOT_MATCHED>
    <ERROR>0</ERROR>
</Graph_Analysis>
<Excel_Analysis>
    <NOT_COMPARED>1</NOT_COMPARED>
    <MATCHED>0</MATCHED>
    <NOT_MATCHED>0</NOT_MATCHED>
    <ERROR>0</ERROR>
</Excel_Analysis>
<Pdf_Analysis>
    <NOT_COMPARED>1</NOT_COMPARED>
    <MATCHED>0</MATCHED>
    <NOT_MATCHED>0</NOT_MATCHED>
    <ERROR>0</ERROR>
</Pdf_Analysis>
</Execution_Stats>

shell script:

#!/bin/bash

 txt=($(grep  '_Analysis' "results.xml"))

 unset(txt[1])
 unset(txt[3])
 unset(txt[5])
 unset(txt[7])
 unset(txt[9])

but i am not getting result as i expected.

I would like to see output like

txt[0]=SQL_Analysis
txt[1]=Data_Analysis
txt[2]=Graph_Analysis
txt[3]=Excel_Analysis
txt[4]=PDF_Analysis

but getting

txt[0]=SQL_Analysis
txt[1]=
txt[2]=Data_Analysis
txt[3]=
txt[4]=Graph_Analysis
txt[5]=
txt[6]=Excel_Analysis
txt[7]=
txt[8]=PDF_Analysis
txt[9]=

Or if there is a way where i cannot select any text which start with </ from the file?

2
  • 1
    Use an XML parser to parse XML. Commented Jun 16, 2017 at 16:55
  • 1
    You could try grep '<[A-Z].*_Analysis' results.xml Commented Jun 16, 2017 at 16:57

1 Answer 1

1

Your syntax is wrong to unset a variable. I'm not even sure how your code is executing as what you have results in a syntax error for me. Your code:

#!/bin/bash

txt=($(grep  '_Analysis' "test.xml"))

unset(txt[1])
unset(txt[3])
unset(txt[5])
unset(txt[7])
unset(txt[9])

Output:

test.sh: line 5: syntax error near unexpected token `txt[1]'
test.sh: line 5: `unset(txt[1])'

See below:

#!/bin/bash

txt=($(grep  '_Analysis' "test.xml"))

unset 'txt[1]'
unset 'txt[3]'
unset 'txt[5]'
unset 'txt[7]'
unset 'txt[9]'

for key in ${txt[@]}; do
    echo $key
done

Which gives me:

dumbledore@ansible1a [OPS]:~ > bash test.sh
<SQL_Analysis>
<Data_Analysis>
<Graph_Analysis>
<Excel_Analysis>
<Pdf_Analysis>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.