awk变量给定错误

awk变量给定错误,awk,Awk,如何在awk中将输出作为变量 电流输出 cat ii Iie:573T Eed:448.0T vail:74T 所需输出 Total Size : 573 TB Total Used : 448.0 TB Avilable : 74 TB Used (%) : 86.10% # Total Used(%) = Total Used/Total Size *100 = 86.1(%) 错误脚本不工作 cat ii | awk ' /Size:/ {total_size=$NF}

如何在awk中将输出作为变量

电流输出

cat ii

Iie:573T
Eed:448.0T
vail:74T
所需输出

Total Size : 573 TB
Total Used : 448.0 TB
Avilable   : 74 TB
Used (%)   : 86.10%   # Total Used(%) = Total Used/Total Size *100 = 86.1(%)
错误脚本不工作

cat ii | awk ' /Size:/ {total_size=$NF}
> /Used:/ { total_used=$NF }
> END{
TotalUsed= total_size=total_used
print "Total Used="TotalUsed}'

要获得
86.1%
您需要,Eed至少
493.353

见:

这里有一种打印修改的标题和百分比的方法

awk 'BEGIN{
           OFS=FS=":";
           h["Iie"]="Total Size";
           h["Eed"]="Total Used";
           h["vail"]="Avilable"
     }
     $1 in h{
           sub(/T/," TB",$2);
           print h[$1], $2; 
           h[$1]=$2 
     }
     END{
           print "Used (%)",h["Eed"]/h["Iie"]*100
     }
    ' infile
输入:

$ cat infile
Iie:573T
Eed:448.0T
vail:74T
输出:

$ awk 'BEGIN{OFS=FS=":";h["Iie"]="Total Size";h["Eed"]="Total Used";h["vail"]="Avilable"}$1 in h{sub(/T/," TB",$2);print h[$1],$2; h[$1]=$2 }END{print "Used (%)",h["Eed"]/h["Iie"]*100}' infile
Total Size:573 TB
Total Used:448.0 TB
Avilable:74 TB
Used (%):78.185
解释

awk 'BEGIN{
           OFS=FS=":";                           # i/p and o/p field sep
           h["Iie"]="Total Size";                # array of key and values
           h["Eed"]="Total Used";
           h["vail"]="Avilable"
     }
     $1 in h{                                    # if its of our interest and in array h
                                                 # not really necessary,
                                                 # in current context
                                                 # skips saving any unwanted column in array h

           sub(/T/," TB",$2);                    # substiute T with space and TB
                                                 # can also be " &B"
           print h[$1], $2;                      # print corresponding header and 2nd field
           h[$1]=$2                              # we are done save value
     }
     END{
           # calculate percentage from saved value
           # 
           print "Used (%)",h["Eed"]/h["Iie"]*100
     }
    ' infile
awk 'BEGIN{
           OFS=FS=":";                           # i/p and o/p field sep
           h["Iie"]="Total Size";                # array of key and values
           h["Eed"]="Total Used";
           h["vail"]="Avilable"
     }
     $1 in h{                                    # if its of our interest and in array h
                                                 # not really necessary,
                                                 # in current context
                                                 # skips saving any unwanted column in array h

           sub(/T/," TB",$2);                    # substiute T with space and TB
                                                 # can also be " &B"
           print h[$1], $2;                      # print corresponding header and 2nd field
           h[$1]=$2                              # we are done save value
     }
     END{
           # calculate percentage from saved value
           # 
           print "Used (%)",h["Eed"]/h["Iie"]*100
     }
    ' infile