awk解析xml

复制代码
?xml version="1.0"?>
<system>
   <machine name="web01" >
         <server name="log_collect">
            <processes>1</processes>
            <threads>1</threads>
            <options>-t1 </options>
            <desc/>
         </server>
         <server name="dcs_fresh">
            <processes>1</processes>
            <threads>1</threads>
            <options>-a192.168.1.1</options>
            <desc/>
         </server>
   </machine>
   <machine name="web02">
         <server name="log_collect">
            <processes>2</processes>
            <threads>2</threads>
            <options>-t2 </options>
            <desc/>
         </server>
         <server name="dcs_fresh">
            <processes>2</processes>
            <threads>2</threads>
            <options>-a192.168.1.2</options>
            <desc/>
         </server>
   </machine>
复制代码
BEGIN{FS="[<>\"]"}
/machine name/{a=$3}
/server name/{b=$3}
/>.*</{c=c?c" "$3:$3}
/<\/server>/{print a" "b" "c;b=0;c=0}
[sunsweet@ubuntu:~/shell/monitor]>>awk -f xml.awk xml
web01 log_collect 1 1 -t1 
web01 dcs_fresh 1 1 -a192.168.1.1
web02 log_collect 2 2 -t2 
web02 dcs_fresh 2 2 -a192.168.1.2

 

你可能感兴趣的:(awk解析xml)