パラメータと値を使用してテキストファイルをiniファイルに変換する

パラメータと値を使用してテキストファイルをiniファイルに変換する

構成ファイルである次のテキストファイルがあります。

advertised.host.name: DEPRECATED: only used when advertised.listeners or listeners are not set. Use advertised.listeners instead. Hostname to publish to ZooKeeper for clients to use. In IaaS environments, this may need to be different from the interface to which the broker binds. If this is not set, it will use the value for host.name if configured. Otherwise it will use the value returned from java.net.InetAddress.getCanonicalHostName().

    Type: string
    Default: node1
    Valid Values:
    Importance: high
    Update Mode: read-only

advertised.listeners: Listeners to publish to ZooKeeper for clients to use, if different than the listeners config property. In IaaS environments, this may need to be different from the interface to which the broker binds. If this is not set, the value for listeners will be used. Unlike listeners it is not valid to advertise the 0.0.0.0 meta-address.

    Type: string
    Default: null
    Valid Values:
    Importance: high
    Update Mode: per-broker

advertised.port: DEPRECATED: only used when advertised.listeners or listeners are not set. Use advertised.listeners instead. The port to publish to ZooKeeper for clients to use. In IaaS environments, this may need to be different from the port to which the broker binds. If this is not set, it will publish the same port that the broker binds to.

    Type: int
    Default: 5500
    Valid Values:
    Importance: high
    Update Mode: read-only

auto.create.topics.enable: Enable auto creation of topic on the server

    Type: boolean
    Default: true
    Valid Values:
    Importance: high
    Update Mode: read-only

.
.
.

私たちが望むのは、上記のファイルを以下のようにiniファイルに変換することです。

advertised.host.name=node1
advertised.listeners=null
advertised.port=5500
auto.create.topics.enable=true
.
.
.

注 - テキストファイルの各パラメータはスペースなしでファイルの先頭にあり、値は次のとおりです。基本

bash、awk、perl / pythonなどを使用してtextファイルをファイルに変換する方法に関する提案があります。ini

ベストアンサー1

そしてawk

$ awk -F': ' '/^[^\t ]+:/{key=$1; next}; $1 ~ /^[\t ]+Default/{print key "=" $2}' file
advertised.host.name=node1
advertised.listeners=null
advertised.port=5500
auto.create.topics.enable=true

おすすめ記事