【问题标题】:executing multipal command in awk function在 awk 函数中执行多个命令
【发布时间】:2017-06-16 14:57:12
【问题描述】:

我们正在获取一个存储报告,我们在其中运行 isi-storagepool --list -v,其中包括一个 awk 函数,该函数进行一些计算并在最后打印数据。

当前工作命令

isi storagepool list -v |awk 'function num2gb(n) { if (n ~ /T$/) return n / 1; return n / 1024; }
        /Requested Protection:/ { parity=substr($NF,length($NF)-1,1) }
        /Nodes:/ { nodes=$NF }
        /HDD Total/ { hdd_total=$NF }
        /HDD Used/ { hdd_used=num2gb($NF) }
        END {
                multiplier=nodes-parity
                total=hdd_total/nodes*multiplier
                used=hdd_used/nodes
                eu=used*multiplier*0.8
                et=total*0.8
                used1=eu/et*100
                print "parity =" parity
                print "NodeNumber =" nodes
                print "Total = " total " TB"
                print "Effective Total volume = " total*0.8 " TB"
                print "USED =" used1 " %"
                print "Effective used=" used*multiplier*0.8 " TB"
                print "Available volume=" (hdd_total-hdd_used)/nodes*multiplier*0.8 " TB" }'

当前工作命令的示例输出

parity =1
NodeNumber =3
Total = 37.3013 TB
Effective Total volume = 29.8411 TB
USED =333975%
Effective used=534360 TB
Available volume=-534330 TB

现在我们想在上面的示例输出中添加更多信息,我们将从以下命令获得。

# isi_classic snapshot usage | tail -n 1
                                                  358G     n/a (R)    0.63% (T)

所以需求输出应该如下

parity =1
NodeNumber =3
Total = 37.3013 TB
Effective Total volume = 29.8411 TB
USED =333975%
Effective used=534360 TB
Available volume=-534330 TB
Snapshot USED = 358G         # added output from the new command  # isi_classic snapshot usage
Snapshot USED % = 0.63%      # added output from the new command  # isi_classic snapshot usage

【问题讨论】:

    标签: shell awk scripting


    【解决方案1】:

    组合多个命令的输出最明显的方法是使用组命令,如下所示:

    { date; date; } | awk 1
    

    一个可能更优雅的解决方案是使用进程替换,如下所示:

    awk 1 <(date) <(date)
    

    例如,后者将允许使用旧的NR==FNR 技巧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      • 2017-11-30
      相关资源
      最近更新 更多