如何建立批次檔以將文字從 .txt 插入到新的 txt 並重複該任務?

如何建立批次檔以將文字從 .txt 插入到新的 txt 並重複該任務?

哇,標題不好,但這就是我想做的事。 Text-file-1 包含:

123.com
234.com
567.com

我需要將這些值插入到新文件的兩個位置,然後將它們移到下一行並插入。

輸出檔看起來像這樣

zone "123.com" IN {
    type master;
    file "/etc/bind/zones/db.123.com";
allow-update { none; };allow-transfer {10.10.10.10; };
};


zone "234.com" IN {
    type master;
    file "/etc/bind/zones/db.234.com";
allow-update { none; };allow-transfer {10.10.10.10; };
};


zone "567.com" IN {
    type master;
    file "/etc/bind/zones/db.567.com";
allow-update { none; };allow-transfer {10.10.10.10; };
};

您可以看到第一個檔案中的網域被插入到結果的 2 個位置。我對批次不熟悉,不知道如何開始。任何幫助真的很感激。

答案1

下面是一個Bashshell 腳本。

#!/bin/bash

while read line
do
    cat <<RECORD
    zone "${line}" IN {
        type master;
            file "/etc/bind/zones/db.${line}";
        allow-update { none; };allow-transfer {10.10.10.10; };
    };

RECORD
done < Text-file-1

還有一個適用於和 的Python版本。LinuxWindows

text = r"""
zone "%s" IN {
            type master;
                file "/etc/bind/zones/db.%s";
                allow-update { none; };allow-transfer {10.10.10.10; };
                };
"""

lines = [ x.strip() for x in open('Text-file-1').readlines() ]

for line in lines:
    print(text % (line, line))

輸出:

zone "123.com" IN {
            type master;
                file "/etc/bind/zones/db.123.com";
                allow-update { none; };allow-transfer {10.10.10.10; };
                };


zone "234.com" IN {
            type master;
                file "/etc/bind/zones/db.234.com";
                allow-update { none; };allow-transfer {10.10.10.10; };
                };


zone "567.com" IN {
            type master;
                file "/etc/bind/zones/db.567.com";
                allow-update { none; };allow-transfer {10.10.10.10; };
                };

相關內容