使用 shell 脚本在 linux 环境中更改 odbcinstini 文件的内容

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

odbcinst.ini 文件的内容是:

# 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

这里我只想修改[Postgresql]下的Driver64行。

我尝试使用 sed 命令来做到这一点

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

但这会改变 Driver64 的每个实例。如果将 [postgresql] 块放在其他地方可能也无济于事。

使用您显示的示例,请尝试遵循 awk 代码,这将仅在 PostgreSQL 之后替换文本,并将保持与替换前行相同的空格。

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

以上将仅在终端上打印输出,一旦您对结果感到满意,您就可以使用以下代码将输出保存到 Input_file 本身。

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