简体   繁体   English

如何在行尾负 1 位置添加带有 sed 的文本

[英]How to add text with sed at the end-of-line minus 1 position

I want to add the text [*,test,*] just before the last character of a line starting with resourceFilters: in the input file (that string can be preceded by any number of spaces).我想在输入文件中以resourceFilters:开头的行的最后一个字符之前添加文本[*,test,*] (该字符串前面可以有任意数量的空格)。

An example of this line is the following此行的示例如下

resourceFilters: '[Event,*,*][*,kube-system,*][*,kube-public,*][*,kube-node-lease,*][Node,*,*][APIService,*,*][TokenReview,*,*][SubjectAccessReview,*,*][Binding,*,*][ReplicaSet,*,*]'

which would be changed into this:将更改为:

resourceFilters: '[Event,*,*][*,kube-system,*][*,kube-public,*][*,kube-node-lease,*][Node,*,*][APIService,*,*][TokenReview,*,*][SubjectAccessReview,*,*][Binding,*,*][ReplicaSet,*,*][*,test,*]'
sed "/^ *resourceFilters: '/s/'$/[*,test,*]'/" your_file
  • I've double quoted the Sed command so that I can use single quotes in it;我已经双引号 Sed 命令,以便我可以在其中使用单引号;
  • /^resourceFilters: '/ makes the following s ubstitution command only act on lines starting with resourceFilters: ' ; /^resourceFilters: '/作出以下s ubstitution命令只能在开始用线起作用resourceFilters: ' ;
  • s/'$/[*,test,*]'/ is the substitution command which matches ' at end of line ( $ ), and substitutes [*, test,*]' to it, effectively inserting [*,test,*] before the ' . s/'$/[*,test,*]'/是匹配'在行尾 ( $ ) 的替换命令,并将[*, test,*]'替换为它,有效插入[*,test,*]'之前。

This might work for you (GNU sed):这可能对你有用(GNU sed):

sed -E 's/^(\s*resourceFilters:.*)(.)$/\1[*,test,*]\2/' file

Match on a line containing resourceFilters: and insert [*,test,*] before the last character of the line.匹配包含resourceFilters:的行,并在该行的最后一个字符前插入[*,test,*]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM