【问题标题】:How to merge ini file with overrides file如何将ini文件与覆盖文件合并
【发布时间】:2019-10-26 20:06:25
【问题描述】:

我正在寻找一种方法,最好是在 bash 中合并两个 ini 文件。这是详细信息。我有一个文件包含“默认值”,另一个文件只指定应更改的行。

例如,我们有文件default.ini 保存默认值:

[foo]
bar=1
baz=2

然后我们有一个单独的文件overrides.ini,其中包含“覆盖”:

bar=10

我想结束的是:

[foo]
bar=10
baz=2

patch 立即浮现在脑海,但我无法让它工作,除非overrides.inidefault.ini 中具有所有值并更改bar(在这种情况下,我不妨只使用overrides.ini

join 似乎是一个选项,直到我看到需要对输入文件进行排序的要求,这在我的情况下是不可能的。

【问题讨论】:

    标签: bash merge ini


    【解决方案1】:

    如果这是 default.ini 中唯一的 bar,您可以使用 awk:

    $ awk -F= 'NR==FNR{a[$1]=$0;next}($1 in a){$0=a[$1]}1' overrides default
    

    输出:

    [foo]
    bar=10
    baz=2
    

    解释:

    $ awk -F= '              # = is the delimiter
    NR==FNR {                # process overrides file
        a[$1]=$0             # hash record, first field is the key
        next                 # process next override entry
    }
    ($1 in a) {              # if ini entry is found in a hash
        $0=a[$1]             # replace with that
    }1' overrides default    # output, mind the file order
    

    【讨论】:

    • 这也适用于 dotenv 文件! awk -F= 'NR==FNR{a[$1]=$0;next}($1 in a){$0=a[$1]}1' .env.production .env
    猜你喜欢
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    相关资源
    最近更新 更多