Linux 如何使用 sed 替换配置文件的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20568515/
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-08-07 01:38:16  来源:igfitidea点击:

How to use sed to replace a config file's variable?

linuxbashreplacesed

提问by SomeGuyOnAComputer

I've been looking online for this answer and cannot seem to find it.

我一直在网上寻找这个答案,但似乎找不到。

I have a config file that contains:

我有一个配置文件,其中包含:

VAR1=VALUE1
VAR2=VALUE2
VAR3=VALUE3
VAR4=VALUE4
VAR5=VALUE5
VAR6=VALUE6

And I want to change VAR5's value from VALUE5 to VALUE10. Unfortunately, I do not know the value of VALUE5 so I cannot search for it. So basically I need to use sed (or whatever) to replace the value of VAR5 to whatever value I want to replace it with.

我想将 VAR5 的值从 VALUE5 更改为 VALUE10。不幸的是,我不知道 VALUE5 的值,所以我无法搜索它。所以基本上我需要使用 sed (或其他)将 VAR5 的值替换为我想替换的任何值。

采纳答案by anubhava

You can try this sed:

你可以试试这个 sed:

sed -i.bak 's/^\(VAR5=\).*/VALUE10/' file

It gives:

它给:

VAR1=VALUE1
VAR2=VALUE2
VAR3=VALUE3
VAR4=VALUE4
VAR5=VALUE10
VAR6=VALUE6

回答by devnull

You can say:

你可以说:

sed '/^VAR5=/s/=.*/=VALUE10/' filename

To make in the change to the file in-place, use the -ioption:

要对文件进行就地更改,请使用以下-i选项:

sed -i '/^VAR5=/s/=.*/=VALUE10/' filename

回答by Vlad.Bachurin

Try the following

尝试以下

sed -r 's/^(VAR5=).*/\1REPLACEMENT/'

sed -r 's/^(VAR5=).*/\1REPLACEMENT/'

The value of VAR5 will be replaced with REPLACEMENT.

VAR5 的值将替换为 REPLACEMENT。

回答by NeronLeVelu

sed '/\(^VAR5=\).*/ s//VALUE10/' YourFile

Under AIX/KSH

在 AIX/KSH 下

$ cat sample.txt
VAR1=VALUE1
VAR2=VALUE2
VAR3=VALUE3
VAR4=VALUE4
VAR5=VALUE5
VAR6=VALUE6

$ sed '/\(^VAR5=\).*/ s//VALUE10/' sample.txt
VAR1=VALUE1
VAR2=VALUE2
VAR3=VALUE3
VAR4=VALUE4
VAR5=VALUE10
VAR6=VALUE6

and for replacement in file

并在文件中替换

cat <> YourFile | sed '/\(^VAR5=\).*/ s//VALUE10/'

$ cat <> sample.txt | sed '/\(^VAR5=\).*/ s//VALUE10/'
$ cat sample.txt
VAR1=VALUE1
VAR2=VALUE2
VAR3=VALUE3
VAR4=VALUE4
VAR5=VALUE10
VAR6=VALUE6

To be POSIX (on sedpart, not cat) compliant (sed --posixon gnu sed and natively traditionnal sed on non linux system)

符合 POSIX(sed部分,不cat)兼容(--posixgnu sed 上的 sed 和非 linux 系统上的本机传统 sed)

回答by Kannan Ramamoorthy

Even though the answer has been added to the question. I spent some time on how it works, I would like add some facts and my version of the answer,

即使答案已添加到问题中。我花了一些时间研究它是如何工作的,我想添加一些事实和我的答案版本,

sed -i 's,^\(THISISMYVARIABLE[ ]*=\).*,'THISISMYVALUE',g' config.cfg

Explanation:

解释:

  • As a basic of sed 's/find_this/replace_with/', we are saying sed to search and replace. Also remember there are multiple other delimiters that we can use instead of /. Here ,is used.
  • Here we find the line that matches ^\(THISISMYVARIABLE[ ]*=\).*. This means we are grouping the match THISISMYVARIABLE[ ]*=. ([ ]*to cover if there are any spaces after the key)
  • In replace section \1is a back-reference. We are referencing the first group in the regular expression that we used for match.
  • 作为 的基础sed 's/find_this/replace_with/',我们说 sed 来搜索和替换。还要记住,我们可以使用多个其他分隔符来代替/. 这里,使用。
  • 在这里我们找到匹配的行^\(THISISMYVARIABLE[ ]*=\).*。这意味着我们正在对比赛进行分组THISISMYVARIABLE[ ]*=。([ ]*如果键后有空格,则覆盖)
  • 在替换部分\1是一个反向引用。我们正在引用我们用于匹配的正则表达式中的第一组。