问题描述
我在my.cnf文件中有一行
datadir = / var / lib / mysql
code>
以下内容将按照预期将其更改为mysql3307。
sed -i's / \ / var \ / lib \ / mysql $ / \ / var \ / lib \ / mysql3307 /'/etc/my.cnf
但如果我有以下情况:
datadir = / var / lib / mysql /
然后下面的内容不会改变如预期的那样:
sed -i's / \ / var \ / lib \ / mysql\ / $ / \ / var\ / lib\ / mysql3307 /'/etc/my.cnf
我想更改mysql3307的路径,其中datadir是/ var / lib / mysql(带或不带/)
更新:
以下是问题:
上述命令都在一台服务器上运行,并且它们都不能在另一台服务器上运行。
预计,但我需要补充说明$以$ / $结尾的行
$ p $ sed -i's / \ / var\ / lib\ / mysql\ / / \ / var\ / lib\ / mysql3307 /'/etc/my.cnf
观察到:
Carot标志^按预期工作,但行尾符号$不符合。任何线索?
更新:
使用dos2unix命令后,它似乎正在工作。
如果您的意图是仅更改具有该特定目录的路径,则需要更复杂一些。你必须在行尾没有尾随 /
的情况下捕捉它,并且你必须在它 结尾<$ c
但是你不想看到之类的东西, / var / lib / mysqlplus
因为这是一个完全不同的目录。
考虑使用扩展正则表达式,并使用一个不同的分隔符,所以你的命令看起来不像锯齿( / \ / \ / \ / \ / \ / \ / \ / \
)。通过这些修改以及对正则表达式本身的小修改,它可以正常工作:
$ echo'
/ var / lib / mysqlplus
/ var / lib / mysql /
/ var / lib / mysql
/ var / lib / mysql / xyz'| sed -E'| / var / lib / mysql([$ /])| / var / lib / mysql3307 \1 |'
/ var / lib / mysqlplus
/ var / lib / mysql3307 /
/ var / lib / mysql
/ var / lib / mysql3307 / xyz
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 sign $ 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
这篇关于逃避美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!