【问题标题】:awk command and variable assignmentawk 命令和变量赋值
【发布时间】:2012-12-12 18:48:14
【问题描述】:

我有一个 MyFile.xml,其内容如下

<root>
    <Main>
            <someothertag>..</someothertag>
        <Amt Ccy="EUR">13</Amt>
    </Main>
                .
                .
                .
                some other tags
    <Main>
          <someothertag>..</someothertag>
             <Amt Ccy="SGD">10</Amt>
    </Main>
    <another>
      <Amt Ccy="EUR">10</Amt>
     </another>
</root>

我有脚本文件,其内容如下

result = `awk '/<Main>/ { f=1 } f && /Amt/ { split($0,a,/[<>]/); s+=a[3] } /<\/Main>/ { f=0 } END {print  s }' MyFile.xml`
echo "The result is " $result

但我得到的输出是

result: 0653-690 Cannot open =.
result: 0653-690 Cannot open 23.
The result is

我的预期输出是

The result is 23

【问题讨论】:

标签: shell unix awk aix


【解决方案1】:

分配变量时,= 的两边都不应有空格。

改为:

result=`awk '/<Main>/ { f=1 } f && /Amt/ { split($0,a,/[<>]/); s+=a[3] } /<\/Main>/ { f=0 } END {print  s }' MyFile.xml`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多