sed command to replace a value in config file
Solution 1
Use sed
:
sed -i 's/\(^Key2=\).*/\1Value4/' file
The -i
flag activates sed
s in-place editing. It searches for a line that begins with Key2=
and replaces the line with the key (\1
) followed by the new value Value4
.
Edit:
If you have slashes or other command characters used in sed
, you have to escape them:
sed -i 's/\(^Key2=\).*/\1long\/value\'/with\$many\"strange\^characters/' /path/to/file
Edit 2: In your case:
sed -i 's/\(^TestT_STOA_TS1901=\).*/\1N/' /app/test/must/untuio.cf
Solution 2
Couple of ways :
Probably the most straight forward way, matching the appropriate key by
/Key2/
and then replacingValue2
withValue4
:$ sed '/Key2/ s/Value2/Value4/' foo.txt Key1=Value1 Key2=Value4 Key3=Value3
If you know that the digits will come at last and if you just want to replace those digit(s) with something else (here replacing
2
with4
) :$ sed '/Key2/ s/[[:digit:]]\+$/4/' foo.txt Key1=Value1 Key2=Value4 Key3=Value3
I also have some other ways on top of my head but they depends on your use case. Let us know if these would suffice.
Your mistakes in sed -i -e '/KEY=/ s/= .*/= new_value/' /path/to/file
:
As per your example, no
Key
will be matched with/KEY=/
as it is not in proper cases and there is a number afterKey
and=
is after thats/= .*/= new_value/
, from your example there is no space after=
but your pattern-substitution seems it has a space in the actual file. Otherwise as far as your given example is concerned there is no space after=
.
Solution 3
Using awk
? =)
awk -F= '$1=="Key2" {printf "%s=%s\n",$1,"Value4"; next}1' your_input_file
Example
The input file foo
cat foo
Key1=Value1
Key2=Value2
Key3=Value3
The command output
% awk -F= ' $1=="Key2" {printf "%s=%s\n",$1,"Value4"; next}1' foo
Key1=Value1
Key2=Value4
Key3=Value3
Related videos on Youtube
canonical
Updated on September 18, 2022Comments
-
canonical over 1 year
I am using this command:
sed -i 's/\(^TestT_STOA_TS1901=\).*/\1N/'/app/test/must/untuio.cf
And I am getting this error:
XXXXXXXXX:/app/test/must>sed -i 's/\(^TestT_STOA_TS1901=\).*/\1N/'/app/test/must/untuio.cf sed: -e expression #1, char 33: unknown option to `s'
Why is this happening? How can I replace a value in a config file?
-
canonical over 8 yearssed -i 's/(^Key2=).*/\1Value4/' file...this didnt work
-
chaos over 8 years@canonical Use the exact command as in my answer. I used backslashes before the brackets.
-
canonical over 8 yearsi get this error : sed: -e expression #1, char 33: unknown option to `s'
-
chaos over 8 years@canonical Has the string you want to replace slashes / in it? (
Value4
in the example). If yes, you need to escape them:sed -i 's/\(^Key2=\).*/\1value\/to\/replace/' file
. Or post the exact command you entered. -
canonical over 8 yearsthis is the exact command i used sed -i 's/(^TestT_STOA_TS1901=).*/\1N/'/app/test/must/untuio.cf I am somehow missing the blackslash when pasting here.But i am actually using it
-
chaos over 8 years@canonical See the edit of my answer. Else we need the exact command and error. Post it in your question or between backticks or see askubuntu.com/editing-help
-
fedorqui over 8 yearsNote you can also say
awk 'BEGIN{FS=OFS="="} $1=="Key2" {$2="Value4"}1' file
so you don't have to "next". -
canonical over 8 yearsI edited the question now
-
chaos over 8 years@canonical Use this:
sed -i 's/\(^TestT_STOA_TS1901=\).*/\1N/' /app/test/must/untuio.cf
. After between the sed command and the file must be a space, else it is interpreted as command, and therefore spits an error. -
A.B. over 8 years@canonical If you like my answer, just click the little grey ☑ on the left hand site of my answer now turning it into beautiful green. If you do not like my answer, click on the little grey down-arrow below the 0, and if you really like my answer, click on the little grey checkmark and the little up-arrow... If you have any further questions, go to askubuntu.com/questions/ask