Monday 15 February 2010

bash - substr in awk statement from xml parse -



bash - substr in awk statement from xml parse -

link original question: bash script extract xml info column format , modification , explanation ->

something within line of code not right , believe substr portion , because don't have total understanding , larn how improve understand it. yes have looked @ documentation , not clicking. couple examples reply helpful.

awk -f'[<>]' 'begin{a["stkpr"]="prod";a["stksvblku"]="prod";a["stksvblock"]="prod";a["stksvblk2"]="test";} /name/{name=$3; type=a[substr(name,length(name))]; if (length(type)==0) type="test";} /sessionhost/+/host/{print type, name, $3;}'|sort -u

this bit here:

type=a[substr(name,length(name))]; if (length(type)==0) type="test";

here xml format each bit block each host contains hostname , ip.

<?xml version="1.0"?> <connection> <connectiontype>putty</connectiontype> <createdby>someone</createdby> <creationdatetime>2014-10-27t11:53:32.0157492-04:00</creationdatetime> <credentialconnectionid>9f3c3bcf-068a-4927-b996-ca52154cae3b</credentialconnectionid> <description>red hat enterprise linux 5 (64-bit)</description> <events> <opencommentprompt>true</opencommentprompt> <warnifalreadyopened>true</warnifalreadyopened> </events> <group>path/to/group/name</group> <id>f2007f03-3b33-47d3-8335-ffd84ccc0e6b</id> <metainformation /> <name>stksprdapp01111</name> <openembedded>true</openembedded> <pinembeddedmode>false</pinembeddedmode> <putty> <alwaysaskforpassword>true</alwaysaskforpassword> <domain>domain</domain> <fontsize>12</fontsize> <host>10.0.0.111</host> <port>22</port> <portfowardingarray /> <telnetencoding>ibm437</telnetencoding> </putty> <stamp>85407098-127d-4d3c-b7fa-8f174cb1e3bd</stamp> <submode>2</submode> <templatename>ssh-perusercreds</templatename> </connection>

what want similar referenced link above. here want match -->

begin{a["stkpr"]="prod";a["stksvblku"]="prod";a["stksvblock"]="prod";a["stksvblk2"]="test";

and of rest test. best read previous post help create 1 more understandable. give thanks you.

because keys here of different length, substr approach less optimal. try:

awk -f'[<>]' '/name/{n=$3;t="test"; if(n ~ /^stkpr/) t="prod"; if (n ~/^stksvblku/) t="prod"; if (n ~/^stksvblock/) t="prod"} /sessionhost/+/host/{print t, n, $3;}' sample.xml |sort -u test stksprdapp01111 10.0.0.111 how works

in case, type, denoted t, set according series of if statements. above code, are:

t="test" if (n ~ /^stkpr/) t="prod" if (n ~ /^stksvblku/) t="prod" if (n ~ /^stksvblock/) t="prod"

by setting t="test", test becomes default: type test unless statement matches. if of next statements looks @ string begins host name and, if there match, sets type t new value. (when regular look begins ^, means follows must match @ origin of string.)

alternative using fancier regular expressions

since above 3 if statements prod type, 3 of them could, if preferred, rearranged to:

t="test" if (n ~ /^stk(pr|svblku|svblock)/) t="prod"

(metalcated: fixed unmatched parentheses bracket)

xml bash awk xml-parsing substr

No comments:

Post a Comment