Sed 用上一行添加新行';s形压痕

Sed 用上一行添加新行';s形压痕,sed,yaml,indentation,Sed,Yaml,Indentation,我现在正在努力更新一些yaml文件,在sed的新行中添加一个元素 sed命令(或另一个linux命令)必须匹配字符串(image:或-image:),在新行中添加一个新元素,该新行的缩进与前一行相同 问题是新行必须正好位于字符串image:下,而不是-image:下 示例:当sed与字符串image匹配时,它会添加具有正确缩进的新行(就在image上方) 种类:守护程序 apiVersion:apps/v1 元数据: 名称:印花布节点 规格: 模板: 规格: 容器: -名称:印花布节点 图片:m

我现在正在努力更新一些yaml文件,在sed的新行中添加一个元素

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

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

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

种类:守护程序
apiVersion:apps/v1
元数据:
名称:印花布节点
规格:
模板:
规格:
容器:
-名称:印花布节点
图片:myimage
imagePullSecret:mysecret
...
示例2:当sed与字符串
-image
匹配时,它会添加具有正确缩进的新行(就在
image
上方,而不是
-
):

种类:守护程序
apiVersion:apps/v1
元数据:
名称:印花布节点
规格:
模板:
规格:
容器:
-图片:myimage
imagePullSecret:mysecret
...
不需要的是

种类:守护程序
apiVersion:apps/v1
元数据:
名称:印花布节点
规格:
模板:
规格:
容器:
-图片:myimage
imagePullSecret:mysecret
我已经尝试了
yq
命令来处理这个问题,但这是一个巨大的痛苦

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

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

我不允许使用
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

我不允许使用
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
我建议:

sed'
/^(*)图像:.*/{p;s/\1imagePullSecret:mysecret/;}
/^(*)-image:.*/{p;s/\1 imagePullSecret:mysecret/;}
“塞德·亚姆
s//replacement/
中的空模式重用最近使用的模式。

我建议:

sed'
/^(*)图像:.*/{p;s/\1imagePullSecret:mysecret/;}
/^(*)-image:.*/{p;s/\1 imagePullSecret:mysecret/;}
“塞德·亚姆

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

使用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

使用GNU awk使用GESUB:

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

您可以使用此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

您可以使用此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

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

yq--tojson eval infle.yaml\
|jq'.spec.template.spec.containers[0]+={imagePullSecret:“mysecret”}\
|yq评估-预打印

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

yq--tojson eval infle.yaml\
|jq'.spec.template.spec.containers[0]+={imagePullSecret:“mysecret”}\
|yq评估-预打印
这可能适合您(GNU-sed):

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

打印当前行

将匹配项替换为
imagePullSecret:mySecret
,并在
-
前面加上两个可选空格。

这可能适合您(GNU-sed):

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

打印当前行


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

打印该行后,需要取消设置rep。这只有在图像位于文件的最后一行时才有效。在我用图像替换图像时,效果非常好:非常感谢!您需要在打印该行后取消设置rep。这只有在图像位于文件的最后一行时才有效。在我用图像替换图像时,效果非常好:非常感谢!