bash - Update odbc.ini file with new parameter in the section using shell scripting - Stack Overflow

admin2025-04-24  2

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

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

Share Improve this question asked Jan 17 at 22:07 user3795378user3795378 254 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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.

转载请注明原文地址:http://anycun.com/QandA/1745454500a90661.html