使用bash转换平面文件数据并将其附加到现有xml文件中



我想弄清楚如何转换平面文件(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> 

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

最新更新