Understanding awk command

时间:2016-05-03 20:10:29

标签: awk gawk

I need help in understanding the following awk command

awk -F "<name>|</name>|<machine>|</machine>" '{if($0 ~ "<name>" && $0 ~ "</name>") nm=$2;else if($0 ~ "<machine>" && $0 ~ "</machine>") {print nm,$2}}' config.xml

This command is giving me the output of weblogic managed servers and their respective hosts in the following format.

managed_server1 host1
managed_server2 host2
managed_server3 host3

1 个答案:

答案 0 :(得分:0)

脚本写得不是特别好,但是从这种格式的行中提取值(按此顺序)

<name>xxx</name>
<machine>yyy</machine>

和输出

xxx yyy

将字段分隔符设置为打开/关闭xml标记,如果行中存在第一对,则将变量设置为第二个字段中的值,当存在​​第二个字段时,打印前一个值集和当前第二个字段。