在AWK声明SUBSTR从XML解析 [英] substr in awk statement from xml parse

查看:102
本文介绍了在AWK声明SUBSTR从XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接到原来的问题:<一href=\"http://stackoverflow.com/questions/26686172/bash-script-extract-xml-data-into-column-format\">bash脚本提取XML数据转换成列格式现在对于修改和说明 - >

这行code之内的东西是不正确的,我相信这是与SUBSTR部分,这将是因为我没有一个充分的认识,并想学习如何更好地去了解它。是的,我已经看过的文件和其没有充分点击。一对夫妇的例子,以及一个答案真的会有所帮助。

 的awk -F[&LT;&GT;]''BEGIN {一[STKPR] =正式版,一个[STKSVBLKU] =正式版;一个[ STKSVBLOCK] =正式版,一个[STKSVBLK2] =测试;} /姓名/名称{= $ 3; TYPE = A [SUBSTR(名称,长度(名))];如果(长度(类型)== 0)类型=测试;} / SessionHost / + /主机/ {打印类型,名称,$ 3;}'|排序-u

这一点在这里:

  TYPE = A [SUBSTR(名称,长度(名))];如果(长度(类型)== 0)类型=测试;

下面是XML格式,每个位是包含主机名和IP每台主机块。

 &LT;?XML版本=1.0&GT?;
&LT;连接GT&;
  &LT; ConnectionType&GT;腻子&LT; / ConnectionType&GT;
  &LT; CreatedBy&GT;有人&LT; / CreatedBy&GT;
  &所述; CreationDateTime&GT; 2014-10-27T11:53:32.0157492-04:00所述; / CreationDateTime&GT;
  &LT; CredentialConnectionID&GT; 9F3C3BCF-068A-4927-B996-​​CA52154CAE3B&LT; / CredentialConnectionID&GT;
  &LT;说明与GT;红帽企业Linux 5(64位)和LT; /说明&GT;
  &LT;活动与GT;
    &LT; OpenCommentPrompt&GT;真&LT; / OpenCommentPrompt&GT;
    &LT; WarnIfAlreadyOpened&GT;真&LT; / WarnIfAlreadyOpened&GT;
  &LT; /新闻与GT;
  &LT;组&gt; PATH / TO / GROUP / NAME&LT; /组&gt;
  &LT;&ID GT; f2007f03-3b33-47d3-8335-ffd84ccc0e6b&LT; / ID&GT;
  &LT;元信息/&GT;
  &LT;名称&gt;&STKSPRDAPP01111 LT; /名称&gt;
  &LT;&OpenEmbedded的GT;真&LT; / OpenEmbedded的&GT;
  &LT; PinEmbeddedMode&GT;假LT; / PinEmbeddedMode&GT;
  &LT;&腻子GT;
    &LT; AlwaysAskForPassword&GT;真&LT; / AlwaysAskForPassword&GT;
    &LT;域名&GT; DOMAIN&LT; /域名&GT;
    &LT;&字号GT; 12&LT; /字号&GT;
    &LT;主机&GT; 10.0.0.111&LT; /主机&GT;
    &LT;港口和GT; 22℃; /端口&GT;
    &LT; PortFowardingArray /&GT;
    &LT; TelnetEncoding&GT;&IBM437 LT; / TelnetEncoding&GT;
  &LT; /腻子&GT;
  &LT;邮票及GT; 85407098-127d-4d3c-b7fa-8f174cb1e3bd&LT; / Stamp和GT;
  &所述;子模式→2&下; /子模式&GT;
  &LT;&TEMPLATENAME GT; SSH-PerUserCreds&LT; / TEMPLATENAME&GT;
&LT; /连接&GT;

我想要做的就是类似上述引用链接。但在这里我想匹配 - >

<$p$p><$c$c>BEGIN{a[\"STKPR\"]=\"Prod\";a[\"STKSVBLKU\"]=\"Prod\";a[\"STKSVBLOCK\"]=\"Prod\";a[\"STKSVBLK2\"]=\"Test\";

和所有其余的测试。最佳读取previous岗位,以帮助使这个更容易理解。谢谢你。


解决方案

由于这里你的钥匙是不同的长度, SUBSTR 办法是不是最佳的。尝试:

 的awk -F[&LT;&GT;]''/姓名/ {N = $ 3; T =测试;如果(N〜/ ^ STKPR /)T =正式版;如果(N〜/ ^ STKSVBLKU /)T =正式版;如果(N〜/ ^ STKSVBLOCK /)T =正式版} / SessionHost / + /主机/ {打印T,N,$ 3;}'sample.xml的|排序-u
测试STKSPRDAPP01111 10.0.0.111

如何使用

在这种情况下,型,由 T 表示,根据如果语句一系列的设置。从上面的code,它们分别是:

  T =测试
如果(N〜/ ^ STKPR /)T =正式版
如果(N〜/ ^ STKSVBLKU /)T =正式版
如果(N〜/ ^ STKSVBLOCK /)T =正式版

通过设置 T =测试测试成为默认:类型为测试,除非另一个语句相匹配。如果下面的语句看开头的主机名字符串,如果有匹配,设置键入 T 来新值。 (当一个普通的前pression始于 ^ ,这意味着接下来必须在字符串的开头相匹配。)

替代使用票友经常EX pressions

由于上述三个如果语句是全部为正式版键入,他们三个可能,如果你preferred,重新排列为:

  T =测试
如果(N〜/ ^ STK(PR | SVBLKU | SVBLOCK)/)T =正式版

(metalcated:固定无与伦比的括号支架)

Link to the original question: bash script extract XML data into column format and now for a modification and explanation ->

Something within this line of code is not correct and I believe it is with the substr portion and that would be because I don't have a full understanding and would like to learn HOW better to understand it. Yes I have looked at documentation and its not fully clicking. A couple examples as well as an answer would really be 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 is the xml format which each bit is a block for each host that contains the hostname and 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 I want to do is similar to the referenced link above. But here I want to match -->

BEGIN{a["STKPR"]="Prod";a["STKSVBLKU"]="Prod";a["STKSVBLOCK"]="Prod";a["STKSVBLK2"]="Test";

and all of the rest as Test. Best to read the previous post to help make this one more understandable. Thank you.

解决方案

Because your keys here are of different length, the substr approach is less than 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 It Works

In this case, the type, denoted by t, is set according to a series of if statements. From the above code, they are:

t="Test"
if (n ~ /^STKPR/) t="Prod"
if (n ~ /^STKSVBLKU/) t="Prod" 
if (n ~ /^STKSVBLOCK/) t="Prod"

By setting t="Test", Test becomes the default: the type will be Test unless another statement matches. If of the following statements looks at the string that begins the host name and, if there is a match, sets type t to a new value. (When a regular expression begins with ^, that means that what follows must match at the beginning of the string.)

Alternative using fancier regular expressions

Since the above three if statements are all for the Prod type, the three of them could, if you preferred, be rearranged to:

t="Test"
if (n ~ /^STK(PR|SVBLKU|SVBLOCK)/) t="Prod"

(metalcated: Fixed unmatched parentheses bracket)

这篇关于在AWK声明SUBSTR从XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆