簡體   English   中英

使用 sed 命令匹配和替換 yaml 文件中的文本的正則表達式

[英]Regex to match and replace text in yaml file using sed command


spec:
  relatedImages:
    - name: apicurio-registry-streams
      image: "registry.stage.xyz.io/integration/apicurio-registry-streams-rhel8@sha256:494e51b1bdf474aa5e6a73d9c0f49d0d471dd44"
    - name: apicurio-registry-jpa
      image: "registry.stage.xyz.io/integration/apicurio-registry-jpa-rhel8@sha256:15abbd968d8d2cb4e7b39c8fab9ded2eb67435ca8bf"
    - name: apicurio-registry-infinispan
      image: "registry.stage.xyz.io/integration/apicurio-registry-infinispan-rhel8@sha256:42bc4f781d50a415d8d91d389f231907dd0253"

我有一個要求,我需要非常頻繁地替換上述 yaml 文件中的圖像引用。 所以基本上我需要像這樣替換第 4,6 和第 8 行雙引號內的 3 個圖像引用:-

替換以下圖片參考:-

registry.stage.xyz.io/integration/apicurio-registry-streams-rhel8@sha256:494e51b1bdf474aa5e6a73d9c0f49d0d471dd44
registry.stage.xyz.io/integration/apicurio-registry-jpa-rhel8@sha256:15abbd968d8d2cb4e7b39c8fab9ded2eb67435ca8bf
registry.stage.xyz.io/integration/apicurio-registry-infinispan-rhel8@sha256:42bc4f781d50a415d8d91d389f231907dd0253

帶有最新的圖片參考:-

registry.stage.xyz.io/integration/apicurio-registry-streams-rhel8@sha256:a57957dad0f9d2ba682a0e4af25804bfdf77ef43
registry.stage.xyz.io/integration/apicurio-registry-jpa-rhel8@sha256:6be04e95d9d627f448f02e8557916973282d4b6cb76
registry.stage.xyz.io/integration/apicurio-registry-infinispan-rhel8@sha256:a682a0e4af25804bfdf77ef43a5f508a547o907

output 文件應該是:-

spec:
  relatedImages:
    - name: apicurio-registry-streams
      image: "registry.stage.xyz.io/integration/apicurio-registry-streams-rhel8@sha256:a57957dad0f9d2ba682a0e4af25804bfdf77ef43"
    - name: apicurio-registry-jpa
      image: "registry.stage.xyz.io/integration/apicurio-registry-jpa-rhel8@sha256:6be04e95d9d627f448f02e8557916973282d4b6cb76"
    - name: apicurio-registry-infinispan
      image: "registry.stage.xyz.io/integration/apicurio-registry-infinispan-rhel8@sha256:a682a0e4af25804bfdf77ef43a5f508a547o907"

有人可以幫我使用 3 個 sed 命令來查找和替換雙引號內的圖像引用,如上所示?

注意:請注意rhel8@sha后面的字母數字值是動態的,不是恆定的。 因此,這樣的解決方案將不起作用:-

sed -e 's|registry.stage.xyz.io/integration/apicurio-registry-streams-rhel8@sha256:494e51b1bdf474aa5e6a73d9c0f49d0d471dd44|registry.stage.xyz.io/integration/apicurio-registry-streams-rhel8@sha256:a57957dad0f9d2ba682a0e4af25804bfdf77ef43|'

我正在尋找更動態的解決方案

根據對原始答案的評論進行了更新:

這是你想要的?

$ cat input.txt
spec:
  relatedImages:
    - name: apicurio-registry-streams
      image: "registry.stage.xyz.io/integration/apicurio-registry-streams-rhel8@sha256:a57957dad0f9d2ba682a0e4af25804bfdf77ef43"
$ sed 's|registry.stage.xyz.io/integration/apicurio-registry-streams-[^"]*|registry.stage.xyz.io/integration/apicurio-registry-streams-rhel8@sha256:494e51b1bdf474aa5e6a73d9c0f49d0d471dd44|' input.txt
spec:
  relatedImages:
    - name: apicurio-registry-streams
      image: "registry.stage.xyz.io/integration/apicurio-registry-streams-rhel8@sha256:494e51b1bdf474aa5e6a73d9c0f49d0d471dd44"
$

這次我們使用正則表達式來獲取 apicurio-registry-streams-之后的任何文本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM