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

时间:2018-02-23 23:44:48

标签: awk

我有一个这样的数组,对于它包含的每个包,包括fail,testcount,error。或者每个包我想提取失败,测试计数和错误的值并在一行中打印

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=\42localhost\42  id=\"0\42  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行,我需要在一行中的结果。 任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

你最好将条件分成他们自己的数组,但缺乏你可以采用更系统的方法

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

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