【问题标题】:Add new line with previous line's indentation使用前一行的缩进添加新行
【发布时间】:2021-01-06 15:11:14
【问题描述】:

我现在一直在努力通过使用 sed 在新行中添加一个元素来更新一些 yaml 文件。

sed 命令(或其他 linux 命令)必须匹配一个字符串(image:- image:),在新行中添加一个与前一行相同缩进的新元素。

问题是新行必须正好在字符串image: 下,而不是- image: 下。

示例:当 sed 匹配字符串 image 时,它会添加具有正确缩进的新行(就在 image 上方)

kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: calico-node
spec:
  template:    
    spec:      
      containers:        
        - name: calico-node
          image: myimage    
          imagePullSecret: mysecret
...

示例 2:当 sed 匹配字符串 - image 时,它会添加具有正确缩进的新行(在 image 上方而不是 - 上方):

kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: calico-node
spec:
  template:    
    spec:      
      containers:
      - image: myimage    
        imagePullSecret: mysecret
...

不想要的是

kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: calico-node
spec:
  template:    
    spec:      
      containers:
      - image: myimage    
      imagePullSecret: mysecret

我已经尝试过yq 命令来处理这个问题,但它是一个巨大的痛苦......

我在另一个线程中找到了此代码,但在匹配 - image 字符串时它不起作用。

sed '/^ *image: .*/ { G; s/^\( *\)image: .*/&\1imagePullSecret: mysecret/; }' sed.yaml

【问题讨论】:

    标签: sed yaml indentation


    【解决方案1】:

    我不容忍使用sed 来解析yaml,但如果你真的想这样做,你可以尝试类似的方法:

    $ nl='                                                                                      
    '
    $ printf '   image:\n     - image:\n' | 
        sed -e "s/\(^\( *\)image:\)/\1\\$nl\2inserted text/" \
        -e "s/\(^\( *\)- image:\)/\1\\$nl\2  inserted text/"
       image:
       inserted text
         - image:
           inserted text
    

    【讨论】:

      【解决方案2】:

      我建议:

      sed '
          /^( *)image: .*/   {p; s//\1imagePullSecret: mysecret/; }
          /^( *)- image: .*/ {p; s//\1  imagePullSecret: mysecret/; }
      ' sed.yaml
      

      s//replacement/ 中的空模式重用了最近使用的模式。

      【讨论】:

        【解决方案3】:

        你可以使用这个 awk:

        awk '/^[[:space:]-]*image/{ split($0,arr,/image.*/) 
                                   gsub(/-/," ", arr[1])
                                   rep=arr[1]
                                   print $0}
                               rep{ printf "%s%s\n", rep, "imagePullSecret: mysecret"
                               rep=""
                               next} 1' file
        

        【讨论】:

        • 您需要在打印该行后取消设置 rep。这仅在图像位于文件的最后一行时才有效。
        • 我用图像替换图像后立即完美工作:非常感谢!
        【解决方案4】:

        使用 GNU awk 来利用 gensub:

        awk '/image: /{ str=$0 } str { print;str1=gensub(/(^.*)(image:.*$)/,"\\1imagePullSecret: mysecret",str);sub("-"," ",str1);print str1;del str;next }1' file
        

        解释:

        awk '/image: /{ # Process the image tag
                         str=$0 # Set a variable str equal to the line
                      } 
                  str { # Process when we have a str set
                         print; # Print the current line
                         str1=gensub(/(^.*)(image:.*$)/,"\\1imagePullSecret: mysecret",str); # Split the line into two sections and set str1 to the first section (get the leading spaces) and then the text we want to add.
                         sub("-"," ",str1); # Replace any - in the text with a space
                         print str1; # Print the new line
                         del str; # Remove the str variable
                         next # Skip to the next line
                       }1' file
        

        【讨论】:

          【解决方案5】:

          Yq v4 尚不支持添加映射,但您可以转换为 JSON,使用 jq,然后再转换回 YAML:

          yq --tojson eval infile.yaml \
              | jq '.spec.template.spec.containers[0] += {imagePullSecret: "mysecret"}' \
              | yq eval -prettyPrint
          

          【讨论】:

            【解决方案6】:

            这可能对你有用(GNU sed):

            sed -E '/(-( ))?image:.*/{p;s//\2\2imagePullSecret: mySecret/}' file
            

            匹配image:.*,并在前面加上可选的-

            打印当前行。

            将匹配项替换为imagePullSecret: mySecret,并在- 前面附加两次之后的可选空格。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-05-05
              • 2020-05-10
              • 2020-07-13
              • 2013-06-03
              • 1970-01-01
              • 1970-01-01
              • 2016-03-21
              相关资源
              最近更新 更多