Search code examples
bashshell

Update odbc.ini file with new parameter in the section using shell scripting


I have an odbc.ini file similar to this

[Test]
Driver=/path/to/driver
Database=test_db

[AnotherSection]
Driver=/another/pathhere

when I run below command, it is adding the Password=5432 at the beginning of the section. Instead I need it at the end of the section [Test]

sed '/^\[Test\]/ a Password=5432}' odbc.ini
[Test]
Password=5432
Driver=/path/to/driver
Database=test_db

[AnotherSection]
Driver=/another/path

expected:

[Test]
Driver=/path/to/driver
Database=test_db
Password=5432

[AnotherSection]
Driver=/another/path

Any suggestions please


Solution

  • With GNU sed:

    sed -e '/\[Test\]/,/^$/{/^$/{i Password=5432' -e '}}' file.ini
    

    Output:

    [Test]
    Driver=/path/to/driver
    Database=test_db
    Password=5432
    
    [AnotherSection]
    Driver=/another/pathhere
    

    I assume that the section is followed by a blank line.