[英]find and replace a string in a file using sed
I have a file odbc.ini
with the following text 我有一个带有以下文本的odbc.ini
文件
[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
我正在尝试用libdb2Wrapper.so
替换此文件中的/home/inst8/sqllib/libdb2o.so
I am trying to do it in a single line using sed but it isn't working. 我正在尝试使用sed在一行中完成此操作,但是它不起作用。 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. 我使用sed 's/libdb2Wrapper.so/~/home/inst8/sqllib/libdb2o.so/g' -i odbc.ini
我也尝试使用反斜杠作为转义字符,但是它不起作用。 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
s不将/
视为第一个字符,则会感到困惑,但大多数人会理解ans逃脱了第一个字符,即
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. 最后,请注意,您可以使用几乎任何字符作为初始reg-ex包装器,只需确保它不在目标文本或替换文本中即可。
IHTH IHTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.