在Linux上使用shell脚本为文本数据生成XML文件

时间:2012-02-05 12:27:13

标签: bash shell

我正在尝试创建file.xml,我有一个包含1000个城市的city.txt文件,每行一个城市名称。

file.xml应该具有city.txt

中每个城市的格式(代码块)
        <ss:Row ss:Height ="22">
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">CITY-FROM-TEXT-FILE-LINE-1</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">CALIFORNIA</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">CA</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
        </ss:Row>

最好的方法是什么?

由于

1 个答案:

答案 0 :(得分:1)

您可以编写awk脚本:

#!/usr/bin/awk -f

BEGIN{
    print "<ss:Row ss:Height='22'>"
}

{
    print "<ss:Cell>"
    print "<ss:Data ss:Type='String'>" $0 "</ss:Data>"
    print "</ss:Cell>"
}

END{
    print "</ss:Row>"    
}

要运行此脚本:

$ awk -f xml.awk city.txt