[英]How to append a line at the end of /etc/sudoers file using shell script (without using pssh tool)
[英]How to replace line if found or append to end of file if not found in shell script?
我提到了帖子:
但是,所有解决方案似乎都没有用。 有人可以解释为什么吗?
我只是通过制作特定文件在终端上执行命令
sed -e 's/^avpgw/new text/' -e t -e 's/^av/new text/' -e t -e 's/^/new text/' file
sed '/^FOOBAR=/{h;s/=.*/=newvalue/};${x;/^$/{s//FOOBAR=newvalue/;H};x}' infile
测试用例:
$ cat > file
match
miss
解决方案:
$ awk 'sub(/match|$/,"hit")' file
hit
misshit
即。 替换第一个match
或记录结尾的$
,以先到者为准。
与sed
:
sed '/match/{s/match/replace/g;p;D}; /match/!{s/$/replace/g;p;D}' file
测试:
$ cat file
some text... match ... some text
some text only here...
..... here also...
$ sed '/match/{s/match/replace/g;p;D}; /match/!{s/$/replace/g;p;D}' file
some text... replace ... some text
some text only here...replace
..... here also...replace
注意:使用-i
在适当位置编辑文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.