Erste verfügbare Nummer aus einer Datei verwenden

Erste verfügbare Nummer aus einer Datei verwenden

Ich habe eine Konfigurationsdateiprosody.config

Mit folgenden Daten:

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"

Ich muss die erste verfügbare Nummer nach der node2.subdomain.domain.comin diesem Fall Nummer finden 3und sie an die gleiche Konfiguration zurücksenden wieecho -e "Component \"node3.subdomain.domain.com\"\n component_secret = \"password\"" >> prosody.config

Der endgültige Inhalt sollte folgendermaßen aussehen:

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"

Bei jedem Ausführen des Skripts erhöht sich die Zahl um eins, ausgehend von der größten Zahl in diesem Fall"node4.subdomain.domain.com"

Danke !

Antwort1

KurzgaffenLösung:

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

Die Ausgabe:

node4

  • FPAT="[0-9]+"- ein regulärer Ausdruck, der mit den Feldern übereinstimmt, anstatt mit dem Feldtrennzeichen

  • END{...}- nur die letzte Zeile der Datei wird berücksichtigt

Antwort2

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

Um die letzte Zeile der Datei zu erhalten, extrahieren Sie die Ziffern am Ende der Zeile und drucken Sie, was den Ziffern vorangeht, gefolgt von der Zahl, aus der diese Ziffern bestehen, erhöht um eins.

Sie können das Ganze auch in erledigen awk, das bedeutet aber, dass die gesamte Datei vollständig gelesen werden muss:

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

Verwenden von shOperatoren:

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

verwandte Informationen