Shell脚本到新文件引用其他文件

时间:2016-07-28 12:34:30

标签: shell

任何人都可以帮助编写shell脚本以满足以下要求。

我有一个CSV文件,如下所示,采用逗号分隔格式

A,B,C
D,E,F

还有另一个文件,其中的值如下所述: -

Ramesh
Suresh

在上述两个文件的帮助下,我需要构建新文件。

<root>
<env name="A" domain="B">
    <component name="Ramesh">
        <machine name="C">Ramesh</machine>
    </component>
    <component name="Suresh">
        <machine name="C">Suresh</machine>
    </component>
</env>
<env name="D" domain="E">
    <component name="Ramesh">
        <machine name="F">Ramesh</machine>
    </component>
    <component name="Suresh">
        <machine name="F">Suresh</machine>
    </component>
</env>
</root>

1 个答案:

答案 0 :(得分:0)

试试这个;

#!/bin/bash
echo "<root>" 
while read csvLine; do 
   echo $csvLine |    awk -F ',' '{print "<env name=\""$1 "\" domain=\"" $2"\">"}' 
   csvcol3=$(echo $csvLine |awk -F ',' '{print $3}')
      while read secondFileLine; do 
         echo -e '\t<component name="'$secondFileLine'">'
         echo -e '\t\t<machine name="'$csvcol3'">'$secondFileLine'</machine>'
         echo -e '\t</component>'
      done < SecondFile.txt
   echo '</env>'
done < file.CSV
echo "</root>"
相关问题