admin管理员组

文章数量:1320864

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.

本文标签: bashUpdate odbcini file with new parameter in the section using shell scriptingStack Overflow