bash - How to read XML and write to text file using shell script? -




how read xml tags , it's value in shell script? searched , found possible when know tag name.

but, want script reads tags in xml(repeated tags), contains values. in xml, tags may change. so, want script can work different xmls.

<data>     <employees>          <employee location = "paris">             <name> andy </name>             <id> 101 </id>         </employee>          <employee location = "germany">             <name> batman </name>             <id> 202 </id>         </employee>      </employees> </data> 

i want read xml , write text file below:

name : alex id: 101 location: paris  name : batman id: 202 location: germany 

try following , let me know if helps you.

awk ' /<\/data>/{   a="";   next } /<data>/{   a=1;   next } /location/ && a{   gsub(/\"|>/,"",$nf);   location=$nf;   next } /name/ && a{   name=$2;   next } /id/ && a{   print "name : ",name rs "id: ",$2 rs "location: ",location;   next } '    input_file 

as per op's request sine no tags should hard coded adding following solution now.

edit2: not xml expert tried here, please check once.

awk 'nf==1 && (/ +<[a-za-z]+>/ || /^<[a-za-z]+>/ || / +<\/[a-za-z]+>/){ next } { sub(/^ +/,"") gsub(/\"|<|>/,"",$0); sub(/\/.*/,""); if($0){   print } } '  input_file 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -