转换平面文件数据并使用bash附加到exisgint xml文件

时间:2014-05-30 13:45:13

标签: bash

我试图弄清楚如何转换平面文件(test_case.dat)有以下细节,需要附加到现有的xml文件(test_xml.xml)

Input files details 
a) test_case.dat 
12123,'QA test case 1','QA environment'   
12234,'UAT test case 1','UAT environment'  

b) test_xml.xml 
<entry> 
    <updated>2014-05-28T19:10:00-07:00</updated> 
    <id>1000573988</id> 
</entry> 
<entry> 
    <updated>2014-05-28T19:10:00-07:00</updated> 
    <id>1000573988</id> 
</entry> 

Expected output 
<entry> 
    <test_id>12123</test_id>    
    <test>QA test case 1</test> 
    <region>QA environment</region> 
    <updated>2014-05-28T19:10:00-07:00</updated> 
    <id>1000573988</id> 
</entry> 
<entry> 
    <test_id>12234</test_id>    
    <test>UAT test case 1</test> 
    <region>UAT environment</region> 
    <updated>2014-05-28T19:10:00-07:00</updated> 
    <id>1000573988</id> 
</entry> 

1 个答案:

答案 0 :(得分:0)

使用awk

#!/bin/awk -f
NR==FNR{
          a[NR]=$0;
          next;
} 
/<entry>/{
          print;
          gsub("'","",a[++i]);
          n=split(a[i],arr,","); 
          if( n!= 3) { next }
          print "\t<test_id>",arr[1],"</test_id>";
          print "\t<test>",arr[2],"</test>";
          print "\t<region>",arr[3],"</region>";
          next;
}1

你可以这样跑,

awk -f awk_script.awk test_case.dat test_xml.xml