Search code examples
shellini

Modifying ini files using shell script


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


Solution

  • 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