开发者

escaping the dollar sign

开发者 https://www.devze.com 2023-03-15 00:48 出处:网络
I have a line in my.cnf file datadir = /var/lib/mysql The following changes it to mysql3307 as expected.

I have a line in my.cnf file

datadir = /var/lib/mysql

The following changes it to mysql3307 as expected.

sed -i 's/\/var\/lib\/mysql$/\/var\/lib\/mysql3307/' /etc/my.cnf

But if I have the following:

datadir = /var/lib/mysql/

Then the following does not change the path as expected:

sed -i 's/\/var\/lib\/mysql\/$/\/var\/lib\/mysql3307/' /etc/my.cnf

I want to change the path to mysql3307 where datadir is /var/lib/mysql (with or without /)

Update:

Here is the issue: Both the above commands works on one server and none of them works on another.

The following works as expected, but I need to add that $ to indicate the lines ending with mysql/

sed -i 's/\/var\/lib\/mysql\//\/var\/lib\/mysql3307/' /etc/my.cnf

Observed:

The carot sign ^ works as expected, but end of line s开发者_如何学Goign $ does not. Any clue?

Update:

It seems to be working after using "dos2unix" command.


If your intent is to only change paths that have that specific directory, you need to be a bit trickier. You have to catch it at the end of a line without the trailing / and you also have to catch it everywhere it has the trailing / (end of line or not).

But you don't want to catch things like /var/lib/mysqlplus since that's a totally different directory.

Consider using extended regular expressions, and using a different separator character so you command doesn't look like sawteeth (/\/\/\/\/\/\/\/\). With those changes, and a small modification to the regular expression itself, it works fine:

$ echo '
/var/lib/mysqlplus
/var/lib/mysql/
/var/lib/mysql
/var/lib/mysql/xyz' | sed -E 's|/var/lib/mysql([$/])|/var/lib/mysql3307\1|'

/var/lib/mysqlplus
/var/lib/mysql3307/
/var/lib/mysql
/var/lib/mysql3307/xyz


The use of $ as a literal character may need to be escaped, but not as an anchor meaning the end of the string.

BTW, your regex would be a lot easier to read if you used a different delimiter, so you didn't have escape the / in the regex

sed -i `'s|/var/lib/mysql/|/var/lib/mysql13307/|' /etc/my.cnf


If you specifically want to change just the datadir value, the following command is more reasonable:
sed -i.bak 's| *datadir *=.*|datadir = /var/lib/mysql3306/|g' /etc/my.cnf

Note: -i.bak the backup of the original file with suffix .bak before making the change.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号