I have an ini
file similar to this
[test]
foo=bar
and if we call this ini
file as test1.ini
How do I change the value of foo
to foobarbaz
for example using shell script
.
I have tried the following and it doesn't work for me. I don't see the updated changes in the ini file. how do I write it?
sed "/^foo=/s/=.*/=foobarbaz/" < test1.ini
Do you have any other suggestions
I personally use a more elaborated sed
command, as the same option might appear in several different sections:
sh$ sed -i.bak '/^\[test]/,/^\[/{s/^foo[[:space:]]*=.*/foo = foobarbaz/}' test1.ini
# ^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# make a in the right perform the substitution
# *backup* section as you want
And as a safety net, I would add:
sh$ diff test1.ini{,.bak}
2c2
< foo = foobarbaz
---
> foo=bar
To have the file updated, use the -i
option of sed:
sed -i "/^foo=/s/=.*/=foobarbaz/" test1.ini
From man sed
:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
So you can also do
sed -i.bak "/^foo=/s/=.*/=foobarbaz/" test1.ini
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With