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.
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,
Explanation:
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.^\(THISISMYVARIABLE[ ]*=\).*
. This means we are grouping the matchTHISISMYVARIABLE[ ]*=
. ([ ]*
to cover if there are any spaces after the key)\1
is a back-reference. We are reference the first group in the regular expression that we used for match.Under AIX/KSH
and for replacement in file
To be POSIX (on
sed
part, notcat
) compliant (sed--posix
on gnu sed and natively traditionnal sed on non linux system)You can try this sed:
It gives:
You can say:
To make in the change to the file in-place, use the
-i
option:Try the following
sed -r 's/^(VAR5=).*/\1REPLACEMENT/'
The value of VAR5 will be replaced with REPLACEMENT.