简体   繁体   English

使用sed,我想替换文件中的字符串值

[英]Using sed, i want to replace the the string value in a file

cat input2.txt

Output: 输出:

    'enable_lb': True,
    'enable_firewall': True,
    'enable_vpn': True,

I tried 我试过了

sed "s/^*.enable_firewall.*/    'enable_firewall': False,/" input2.txt

It's not changing the value of firewall to false, please help me out in this syntax 它不会将防火墙的值更改为false,请使用以下语法帮助我

Thanks in Advance 提前致谢

You need to put . 你需要放. before the * , so that it would match any char zero or more times. *之前的值,这样它将与任何char匹配零次或更多次。

sed "s/^.*enable_firewall.*/    'enable_firewall': False,/" input2.txt

To do an in-place edit, add -i parameter. 要进行就地编辑,请添加-i参数。

sed -i "s/^.*enable_firewall.*/    'enable_firewall': False,/" input2.txt

To be more precise: 更准确地说:

sed "s/\('enable_firewall': \)True/\1False/g" file

This matches exactly 'enable_firewall': True 这与'enable_firewall': True完全匹配'enable_firewall': True

Or: 要么:

sed "s/\('enable_firewall':.*\)True/\1False/g" file

This matches enable_firewall':*True * means anything 匹配enable_firewall':*True *表示任何含义

Or: 要么:

sed "s/\('enable_firewall':[ \t]*\)True/\1False/g" file1

This matches 'enable_firewall': True with zero or more spaces or tabs between : and True . 这与'enable_firewall': True相匹配,在:True之间有零个或多个空格或制表符。 If at least one space is a must, use [ \\t]\\+ instead of [ \\t]* 如果必须至少有一个空格,请使用[ \\t]\\+而不是[ \\t]*

Note: use sed -i ... to replace inplace in file 注意:使用sed -i ...替换文件中的原位

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

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