shell - Delete specific XML elements after finding specific test condition within the element start/end tag -


i'm looking create quick shell script (hp-ux system) remove xml tags based on simple test condition. can't use xml aware tools 'xmlstarlet' these not available on production systems number of reasons. realise these right way go, i've no choice in matter.

consider 2 xml elements below relating 2 devices. when device out-of-comms there no stationid , no hardwareinv, tags differ <..../> @ end. when device in comms, stationid present , hardwareinv content available, start/end tags complete, i.e. </....> @ end.

i find , remove devices out-of-comms searching <stationid/> and/or <hardwareinv/> , if found, delete content between associated devicea tags, including devicea tags leaving no blank line behind.

i have tried few things varying results using 'sed' in particular, nothing 100% successful. appreciated.

this input xml file:

<devicea>   <physicaladd>10.10.10.69</physicaladd>   <neid>0000-test-06</neid>   <stationid/>    *** more content removed ***    <hardwareinv/> </devicea> <devicea>   <physicaladd>10.10.10.109</physicaladd>   <neid>0000-test-13</neid>   <stationid>bravo-01</stationid>    *** more content removed ***    <hardwareinv>     <unit>       <unitid>1</unitid>       <serialnumber>1389a</serialnumber>     </unit>   </hardwareinv> </devicea> 

the expected output:

<devicea>   <physicaladd>10.10.10.109</physicaladd>   <neid>0000-test-13</neid>   <stationid>bravo-01</stationid>    *** more content removed ***    <hardwareinv>     <unit>       <unitid>1</unitid>       <serialnumber>1389a</serialnumber>     </unit>   </hardwareinv> </devicea> 

this script simple enough work version of awk:

awk ' /<devicea>/          { found = 0; tosave = 1; save = "" } /<hardwareinv\/>/ || /<stationid\/>/        { found = 1 } /<devicea>/,/<\/devicea>/   { save = save $0 "\n" } tosave==0            { print } /<\/devicea>/        { if(!found)printf "%s",save; tosave = 0 } ' 

it detects starting tag , sets 2 booleans false, 0, , true, 1, , clears out string variable save.
when empty tags found, found boolean set true. lines between start , end tag of group removed accumulated in string variable, newline between them.

if not saving lines, print them. when end tag matched, if empty tags not found, print saved group, , stop saving.

there redundancy in code, keep simple. obviously, handles data in format gave, , not xml.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -