Sunday, March 13, 2022

[SOLVED] Changing contents of a odbcinstini file in linux environment using shell script

Issue

The content of the odbcinst.ini file is :

# Example driver definitions

# Driver from the postgresql-odbc package
# Setup from the unixODBC package
[PostgreSQL]
Description     = ODBC for PostgreSQL
Driver          = /usr/lib/psqlodbcw.so
Setup           = /usr/lib/libodbcpsqlS.so
Driver64        = /usr/lib64/psqlodbcw.so
Setup64         = /usr/lib64/libodbcpsqlS.so
FileUsage       = 1


# Driver from the mysql-connector-odbc package
# Setup from the unixODBC package
[MySQL]
Description     = ODBC for MySQL
Driver          = /usr/lib/libmyodbc5.so
Setup           = /usr/lib/libodbcmyS.so
Driver64        = /usr/lib64/libmyodbc5.so
Setup64         = /usr/lib64/libodbcmyS.so
FileUsage       = 1

Here I want to modify only Driver64 line under [Postgresql].

I tried to do it using sed command

sed -i 's/Driver64.*/Driver64=/usr/some/path/'

But this will change every instance of Driver64. And it might not help if [postgresql] block is placed somewhere else.


Solution

With your shown samples, please try following awk code, this will substitute text only after PostgreSQL and will maintain same spaces which line was having before substitution also.

awk -v newPath="your_new_path" '
/\[PostgreSQL\]/{ found=1 }
found && /Driver64/{
  match($0,/^.*=[[:space:]]+/)
  $0=substr($0,RSTART,RLENGTH) newPath
}
1
' Input_file 

Above will print output on terminal only, once you are Happy with results then you can use following code to save output into Input_file itself.

awk -v newPath="your_new_path" '
/\[PostgreSQL\]/{ found=1 }
found && /Driver64/{
  match($0,/^.*=[[:space:]]+/)
  $0=substr($0,RSTART,RLENGTH) newPath
}
1
' Input_file > temp && mv temp Input_file


Answered By - RavinderSingh13
Answer Checked By - Timothy Miller (WPSolving Admin)