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

发布于 2025-01-02 19:37:04 字数 3339 浏览 0 评论 0原文

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

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>

最好的方法是什么?

谢谢

I am trying to create file.xml, I have a city.txt file with 1000 cities, one city name per line.

The file.xml should have this format (code block) for each city from 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>

What would be the best way to do that ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韬韬不绝 2025-01-09 19:37:04

您可以编写一个 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

You can write a awk script:

#!/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>"    
}

To run this script:

$ awk -f xml.awk city.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文