【问题标题】:creating multiple files from template and replace content on-the-fly从模板创建多个文件并即时替换内容
【发布时间】:2018-10-12 20:53:12
【问题描述】:

如标题所述,我想从模板创建多个文件并即时替换固定关键字

如果我这样做了

$ sed s/XX/{01..05}/g templates/pXX.conf

我有点接近了,因为我得到了第一个结果的正确输出,但数字 02-04 的输出错误。

由于在 icinga 中不需要分隔每个配置文件(但这将是一个额外的好处),因此可以将结果放入单个输出文件中。

例子:

//template.conf
object Host "pXX" {
  display_name = "RasPi XX"
  ...
}

这现在应该导致:

//p01.conf <- ascending filenames would be a bonus
object Host "p01" {
  display_name = "RasPi 01"
  ...
}
//p02.conf
object Host "p02" {
  display_name = "RasPi 02"
  ...
}
// and so on

我很确定这很容易通过使用带有循环的任何类型的脚本来完成

while $i < number: read file; replace content; output file;

我只是好奇这是否可以使用一些单行命令来完成

【问题讨论】:

    标签: linux bash sed nagios icinga


    【解决方案1】:
    for i in 01 02 03; do sed "s/XX/$i/" template.conf > template$i.conf; done
    

    【讨论】:

    • 有效。甜的! (“for i in {01..99}”使其可扩展)
    • @EBZ-Krisemendt:非常好的一点——尽管必须小心前导“0”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    相关资源
    最近更新 更多