ファイルから利用可能な最初の番号を使用する

ファイルから利用可能な最初の番号を使用する

設定ファイルがありますprosody.config

以下のデータ:

VirtualHost "pubsub.subdomain.domain.com"
admins = { "node1.subdomain.domain.com", "node2.subdomain.domain.com" }
    autocreate_on_subscribe = true
    autocreate_on_publish = true
    modules_enabled = {
        "pubsub";
    }

VirtualHost "subdomain.domain.com"
    authentication = "anonymous"
    modules_enabled = {
        "bosh";
    }
    c2s_require_encryption = false

VirtualHost "auth.subdomain.domain.com"
    authentication = "internal_plain"

admins = { "[email protected]" }

Component "node1.subdomain.domain.com"
    component_secret = "password"
Component "node2.subdomain.domain.com"
    component_secret = "password"
Component "conference.subdomain.domain.com" "muc"
Component "focus.subdomain.domain.com"
    component_secret = "password"

node2.subdomain.domain.comこの場合の番号の後に利用可能な最初の番号を見つけて3、同じ構成にエコーバックする必要があります。echo -e "Component \"node3.subdomain.domain.com\"\n component_secret = \"password\"" >> prosody.config

最終的な内容は次のようになります。

VirtualHost "pubsub.subdomain.domain.com"
    admins = { "node1.subdomain.domain.com", "node2.subdomain.domain.com" }
        autocreate_on_subscribe = true
        autocreate_on_publish = true
        modules_enabled = {
            "pubsub";
        }

    VirtualHost "subdomain.domain.com"
        authentication = "anonymous"
        modules_enabled = {
            "bosh";
        }
        c2s_require_encryption = false

    VirtualHost "auth.subdomain.domain.com"
        authentication = "internal_plain"

    admins = { "[email protected]" }

    Component "node1.subdomain.domain.com"
        component_secret = "password"
    Component "node2.subdomain.domain.com"
        component_secret = "password"
    Component "conference.subdomain.domain.com" "muc"
    Component "focus.subdomain.domain.com"
        component_secret = "password"
    Component "node3.subdomain.domain.com"
        component_secret = "password"
    Component "node4.subdomain.domain.com"
        component_secret = "password"

この場合、スクリプトを実行するたびに、数字は最大値から1ずつ増加します。"node4.subdomain.domain.com"

ありがとう !

答え1

短い驚いて見る解決:

awk -v FPAT="[0-9]+" 'END{print "node"$1+1}' xyz.config

出力:

node4

  • FPAT="[0-9]+"- フィールド区切り文字に一致するのではなく、フィールドに一致する正規表現

  • END{...}- ファイルの最後の行のみを考慮する

答え2

< xyz.config tail -n 1 | awk 'match($0, /[0-9]+$/) {
  print substr($0, 1, RSTART-1) substr($0, RSTART)+1}'

ファイルの最後の行を取得するには、行末の数字を抽出し、数字の前の部分を出力し、その後に 1 ずつ増加した数字を出力します。

すべてを で実行することもできますawkが、その場合はファイル全体を完全に読み取る必要があります。

< xyz.config awk 'END {if(match($0, /[0-9]+$/))
  print substr($0, 1, RSTART-1) substr($0, RSTART)+1}'

演算子の使用sh:

< xyz.config tail -n1 | (
  IFS= read -r line || exit
  num=${line##*[!0-9]}
  printf '%s\n' "${line%"$num"}$((num + 1))")

関連情報