简体   繁体   中英

find and replace a string in a file using sed

I have a file odbc.ini with the following text

[DB2DB]
DRIVER=libdb2Wrapper.so
Description=DB2DB DB2 ODBC Database
Database=DB2DB
[EBAICIMS]
DRIVER=libdb2Wrapper.so
Description=DB ALIAS 
Database=EBAICIMS
[EBAIPDMD]
DRIVER=libdb2Wrapper.so
Description=DB ALIAS 
Database=EBAIPDMD
[EBAIRELS]
DRIVER=libdb2Wrapper.so
Description=DB ALIAS 
Database=EBAIRELS
[POLICYD]
DRIVER=libdb2Wrapper.so
Description=DB2 alias to connect
Database=POLICYD
[JDCUCCD]
DRIVER=libdb2Wrapper.so
Description=DB2DB DB2 ODBC Database
Database=JDCUCCD
[POLICYQ]
DRIVER=libdb2Wrapper.so
Description=DB2 alias to connect
Database=POLICYQ
[POLICYM]
DRIVER=libdb2Wrapper.so
Description=DB2 alias to connect
Database=POLICYM

I am trying to replace libdb2Wrapper.so in this file with /home/inst8/sqllib/libdb2o.so

I am trying to do it in a single line using sed but it isn't working. I used sed 's/libdb2Wrapper.so/~/home/inst8/sqllib/libdb2o.so/g' -i odbc.ini I also tried with backslashes as escape characters but it didn't work. Maybe I am doing something wrong with the use of escape characters. Please help.

为了避免反斜杠的需要,请对sed使用不同的分隔符:

sed -i~ -e 's=libdb2Wrapper.so=~/home/inst8/sqllib/libdb2o.so=g' odbc.ini

While you don't need to show every attempt to solve your problem, as you realize it is probably escaping / chars that is your problem, best to include that attempt in your question.

Try

sed -i '@libdb2Wrapper.so@\~/home/inst8/sqllib/libdb2o.so@g' odbc.ini

You probably need to esacpe the ~ char as it is usually means "all that was matched"

Some older sed s get confused if they don't see / as the first character, but most will understand ans escaped first char, ie

sed -i '\@libdb2Wrapper.so@\~/home/inst8/sqllib/libdb2o.so@g' odbc.ini  

Finally, note that you can use about any character as the initial reg-ex wrapper, just make sure it's not in either your target or replacement text.

IHTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM