【问题标题】:Append/supplement data from file1 to file2 (linux)将文件 1 中的数据追加/补充到文件 2 (linux)
【发布时间】:2021-04-29 19:10:05
【问题描述】:

我有一个问题,有人可以建议或给出一些 bash 脚本示例如何将数据从 file1 填充到 file2。下面是需要传输到file2的数据。

FILE1 信息

// -- ALL LINES BELOW WERE GENERATED --
// -- Some custom text that can be anywhere --
zone "domain40.com" {
    type master;
    file "domain4.com";
    allow-transfer {
            xx.xx.xx.xx;
            2203:d1d0:0:4swd::1;
            common-allow-transfer;
    };
    };
 zone "domain50.com" {
    type master;
    file "domain5.com";
    allow-transfer {
            xx.xx.xx.xx;
            2203:d1d0:0:4swd::1;
            common-allow-transfer;
    };
    };
 ..............

FILE2 信息

zone "domain10.com" {
    type master;
    file "domain10.com";
    allow-transfer {
            xx.xx.xx.xx;
            2203:d1d0:0:4swd::1;
            common-allow-transfer;
    };
    };
 zone "domain12.com" {
    type master;
    file "domain12.com";
    allow-transfer {
            xx.xx.xx.xx;
            2203:d1d0:0:4swd::1;
            common-allow-transfer;
    };
    };
 zone "domain13.com" {
    type master;
    file "domain13.com";
    allow-transfer {
            xx.xx.xx.xx;
            2203:d1d0:0:4swd::1;
            common-allow-transfer;
    };
    };
 ..............

所以问题是如何在 linux 中合并文件,仅将数据从 FILE1 添加到 FILE2 从 zone 到 };没有任何其他文本,例如 // -- 下面的所有行都已生成 --。
我需要将该数据从导入的 FILE1 添加到现有文件 FILE2。有什么帮助吗?

【问题讨论】:

  • cat file1 file2 > file.new
  • cat file1 >> file2将file1信息添加到file2的末尾。
  • @Barmar 是的,我知道 cat 可以做到,但问题是它会将文件 1 中的所有内容复制到文件 2 中,并带有不需要的文本。
  • 那么您需要在问题中更加具体。显示示例输入和所需的结果。
  • sed -n '/startpattern/,/endpattern/p' file1 >> file2

标签: linux bash ubuntu


【解决方案1】:

如果自动生成的文本以“/”开头,那么您可以使用例如删除这些行

grep -v "/" file1 >> file2

这能解决你的问题吗?

【讨论】:

  • 是的,谢谢,@Barmar 也通过他的提示解决了这个问题!
猜你喜欢
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
相关资源
最近更新 更多