awk结果错误,数组作为变量

时间:2014-04-02 14:52:45

标签: arrays unix awk

我必须解析输入的webserver日志文件,并且它们保存有关每个请求的信息。我必须得到它的中位数。我想把所有这些间隔保存在一个数组中,对它进行排序并将mid元素从中返回。作为第一步,我试图收集数组中的所有间隔,但看起来像awk有数组问题。请让我知道脚本有什么问题,我收到错误,如非法引用变量intvArray 。有人可以检查intvArray的问题是什么

脚本如下

#!/bin/bash

rm -rf 0.out 1.out 2.out collection.out parsed.out
scp root@10.XX.XX.XX4:/opt/tomcat/escr/log/rce_reactive_001.out ./0.out;
scp root@10.XX.XX.XX5:/opt/tomcat/escr/log/rce_reactive_002.out ./1.out;
scp root@10.XX.XX.XX3:/opt/tomcat/escr/log/rce_reactive_000.out ./2.out;
scp root@10.XX.XX.XX7:/opt/tomcat/escr/log/rce_reactive_003.out ./3.out;

cat ./0.out ./1.out 2.out 3.out >> ./collection.out;
grep interval ./collection.out >> ./parsed.out;

sum=0; count=1; intvArray=(0 0);

#awk 'BEGIN {if($12 + 0 == $12){ sum+=$12; count++}} END{  print sum;}' ./parsed.out
#awk 'BEGIN {sum=0; count=0;} {if($12 + 0 == $12){ sum += $12; count++;}} END{print "Count", count, "Average:", sum/count}' ./parsed.out
awk 'BEGIN {sum=0; count=1;intvArray=(0 0);} {if($12 + 0 == $12){ intvArray[count]=$12; count++;}} END{print "Count", count, "Array:", intvArray}' ./parsed.out

#for a in "${intvArray[@]}"; do echo "$a"; done

2 个答案:

答案 0 :(得分:2)

对代码进行一些简化 - 没有看到您的输入:

#!/bin/bash

rm -rf ?.out collection.out parsed.out

scp root@10.XX.XX.XX4:/opt/tomcat/escr/log/rce_reactive_001.out 0.out
scp root@10.XX.XX.XX5:/opt/tomcat/escr/log/rce_reactive_002.out 1.out
scp root@10.XX.XX.XX3:/opt/tomcat/escr/log/rce_reactive_000.out 2.out
scp root@10.XX.XX.XX7:/opt/tomcat/escr/log/rce_reactive_003.out 3.out

cat {0..3}.out | grep interval > parsed.out

awk 'BEGIN {sum=0; count=0;} {if($12 + 0 == $12){ sum += $12; count++;}} END{print "Count", count, "Average:", sum/count}' parsed.out

awk '{if($12 + 0 == $12)iv[++count]=$12} END{print "Count", count;for(i in iv) print "iv[",i,"] ",iv[i]}' parsed.out

感谢Ed Morton的简化和改进,他建议如下。我已将它们添加到我的答案的主体中,这样所有人都可以轻松地看到它们并且格式很好:

awk '$12 + 0 == $12{sum+=$12;count++} END{print "Count",count,"Average:", sum/count}' parsed.out

以及

awk '$12 + 0 == $12{iv[++count]=$12} END{print "Count", count;for(i in iv) printf "iv[%d] %d\n",i,iv[i]}' parsed.out

答案 1 :(得分:2)

您可以在没有临时文件的情况下完成此操作:

{
    ssh root@10.XX.XX.XX4 cat /opt/tomcat/escr/log/rce_reactive_001.out
    ssh root@10.XX.XX.XX5 cat /opt/tomcat/escr/log/rce_reactive_002.out
    ssh root@10.XX.XX.XX3 cat /opt/tomcat/escr/log/rce_reactive_000.out
    ssh root@10.XX.XX.XX7 cat /opt/tomcat/escr/log/rce_reactive_003.out
} |
awk '
    /interval/ && $12 == $12 + 0 {intvArray[count++] = $12} 
    END {
        print "Count", count, "Array:"
        for (idx=0; idx<count; idx++) print idx, intvArray[idx]
    }
'

现在,如果你想在bash数组中使用awk数组:

intvArray=( $(
    {   ssh root@10.XX.XX.XX4 cat /opt/tomcat/escr/log/rce_reactive_001.out
        ssh root@10.XX.XX.XX5 cat /opt/tomcat/escr/log/rce_reactive_002.out
        ssh root@10.XX.XX.XX3 cat /opt/tomcat/escr/log/rce_reactive_000.out
        ssh root@10.XX.XX.XX7 cat /opt/tomcat/escr/log/rce_reactive_003.out
    } | awk '/interval/ && $12 == $12 + 0 {print $12}'
) )