从一行中提取awk数组中的值

  • 本文关键字:提取 awk 数组 一行 awk
  • 更新时间 :
  • 英文 :


我都有这样的数组,对于每个软件包,它持有失败,测试扣,错误的计数。或我想提取失败,测试计数和错误的值,并在一行中打印的值

test_result["012_project_y2014","fails"] = 1;     
test_result["012_project_y2014","testcount"] =3;    
test_result["012_project_y2014", "error"] = 1;  
test_result["012_project_y2013","fails"] = 0;             
test_result["012_project_y2013","testcount"]=1;    
test_result["012_project_y2013", "error"] = 1;  


    for (y in test_result){
            split(y,sep1,SUBSEP);

            pkg = sep1[1];
            result =  sep1[2];
            if (result == "testcase")
            {
                    ptest = test_result[sep1[1],sep1[2]];
            }
            if (result == "fail")
            {
                    pfail = test_result[sep1[1],sep1[2]];
            }
            if(result == "error")
            {
                    perror = test_result[sep1[1],sep1[2]];
            }
            count = test_result[sep1[1],sep1[2]];

            print "<testsuite errors=42"perror"42"" " "failures=42"pfail"42"" " "hostname=42localhost42  id="042  name=42"pkg"42"" ""package=42"pkg"42"" " "tests=42"ptest"42"" " "timestamp=42"date"42"">n";

}

我获得的输出如下所示,每个计数都在打印测试套件,即失败,测试计数,错误它打印一行

<testsuite errors="" failures="" hostname="localhost"  id="0"  
name="012_project_y2014 " package="012_project_y2014 " tests="1" 
timestamp="">  

<testsuite errors="" failures="0" hostname="localhost"  id="0"  
name="012_project_y2014 " package="012_project_y2014 " tests="1" 
timestamp="">  

<testsuite errors="" failures="1" hostname="localhost"  id="0"  
name="012_project_y2013 " package="012_project_y2013 " tests="1" 
timestamp="">  

<testsuite errors="1" failures="1" hostname="localhost"  id="0"  
name="012_project_y2014 " package="012_project_y2014 " tests="1" 
timestamp="">  
<testsuite errors="1" failures="1" hostname="localhost"  id="0"  
name="012_project_y2013 " package="012_project_y2013 " tests="3" 
timestamp="">  
<testsuite errors="1" failures="1" hostname="localhost"  id="0" 
name="012_project_y2013 " package="012_project_y2013 " tests="3" 
timestamp="">  

预期输出如下所示,我可以访问所有失败,测试范围,一行中的软件包错误和打印

    <testsuite errors="" failures="1" hostname="localhost"  id="0"  name="012_project_y2013 " package="012_project_y2013 " tests="1" timestamp="">
    <testsuite errors="1" failures="1" hostname="localhost"  id="0"  name="012_project_y2013 " package="012_project_y2013 " tests="3" timestamp="">

还是有一种方法可以以不同的方式处理此数组,以获得预期的结果。我认为上面没有什么效法的事情,我的结果越来越接近,但是打印3行我需要一行结果。任何帮助都将受到赞赏

您最好将条件分隔为自己的数组,但是缺乏您可以使用更系统的方法

test_result["012_project_y2014","fails"] = 1;     
test_result["012_project_y2014","testcount"] = 3;    
test_result["012_project_y2014","error"] = 1;  
test_result["012_project_y2013","fails"] = 0;             
test_result["012_project_y2013","testcount"] = 1;    
test_result["012_project_y2013","error"] = 1;  
for (y in test_result) {
  split(y,ss,SUBSEP);
  names[ss[1]]; status[ss[2]];
}
for(n in names) {
  printf "name=%s", n;
  for(s in status) printf "%s=%s", OFS s, test_result[n,s];
  print "";
}
...

会给你

name=012_project_y2013 fails=0 error=1 testcount=1
name=012_project_y2014 fails=1 error=1 testcount=3

您可以根据需要进一步格式化结果...

最新更新