bash 使用 Sed 更改文本文件中的变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22229365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:48:47 来源:igfitidea点击:
Change variable value inside text file with Sed
提问by HobbitOfShire
I have a text file containting this
我有一个包含这个的文本文件
Var1=ofzer
Var2=smelf
..
..
VarN=mskfm
I want to change the value of one of my variables using Sed. How is that possible?
我想使用 Sed 更改我的变量之一的值。这怎么可能?
回答by problemPotato
Say you wanted to change the value of Var2 from 'smelf' to 'smurf', you could use:
假设您想将 Var2 的值从 'smelf' 更改为 'smurf',您可以使用:
bash$ sed -i 's/Var2=.*/Var2=smurf/' file.txt
回答by andrey
In case you want more complex substitution:
如果您想要更复杂的替换:
sed_param=s/Var1=.*/Var1=${NEW_VALUE}/
sed -i "$sed_param" file.txt