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

时间:2021-07-24 16:51:09

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的值替换为我想要替换的值。

5 个解决方案

#1


36  

You can try this sed:

你可以试试这个sed:

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

It gives:

它给:

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

#2


4  

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[ ]*=\).*,\1'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.
  • 作为sed 's/find_this/replace_with/的基础,我们说sed要搜索和替换。还要记住,我们可以使用多个其他分隔符来代替/。在这里,使用。
  • 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)
  • 在这里,我们找到匹配的行^ \(THISISMYVARIABLE[]* = \)。*。这意味着我们正在对匹配的THISISMYVARIABLE[]*=进行分组。([]*在关键字后面有空格)
  • In replace section \1 is a back-reference. We are reference the first group in the regular expression that we used for match.
  • 在replace部分\1是一个反向引用。我们引用正则表达式中用于匹配的第一个组。

#3


3  

You can say:

你可以说:

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

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

若要更改文件的位置,请使用-i选项:

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

#4


1  

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

Under AIX/KSH

在AIX / KSH

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

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

and for replacement in file

以及更换文件。

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

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

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

是POSIX(在sed部分,而不是cat)兼容的(sed——在gnu sed上是POSIX,在非linux系统上是本地传统的sed)

#5


0  

Try the following

试试以下

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

sed - r ' s / ^(VAR5 =)。* / \ 1替换/ '

The value of VAR5 will be replaced with REPLACEMENT.

VAR5的值将被替换为替换。

#1


36  

You can try this sed:

你可以试试这个sed:

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

It gives:

它给:

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

#2


4  

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[ ]*=\).*,\1'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.
  • 作为sed 's/find_this/replace_with/的基础,我们说sed要搜索和替换。还要记住,我们可以使用多个其他分隔符来代替/。在这里,使用。
  • 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)
  • 在这里,我们找到匹配的行^ \(THISISMYVARIABLE[]* = \)。*。这意味着我们正在对匹配的THISISMYVARIABLE[]*=进行分组。([]*在关键字后面有空格)
  • In replace section \1 is a back-reference. We are reference the first group in the regular expression that we used for match.
  • 在replace部分\1是一个反向引用。我们引用正则表达式中用于匹配的第一个组。

#3


3  

You can say:

你可以说:

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

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

若要更改文件的位置,请使用-i选项:

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

#4


1  

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

Under AIX/KSH

在AIX / KSH

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

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

and for replacement in file

以及更换文件。

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

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

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

是POSIX(在sed部分,而不是cat)兼容的(sed——在gnu sed上是POSIX,在非linux系统上是本地传统的sed)

#5


0  

Try the following

试试以下

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

sed - r ' s / ^(VAR5 =)。* / \ 1替换/ '

The value of VAR5 will be replaced with REPLACEMENT.

VAR5的值将被替换为替换。