sed在处理某些字符串字符时无法工作。

huangapple go评论51阅读模式
英文:

sed is not working with certain string characters

问题

我一直在阅读各种Sed问题,有一个问题我无法解决:

我想替换:

define('CIVICRM_LOGGING_DSN', CIVICRM_DSN);

define('CIVICRM_LOGGING_DSN', myuser:mypassword@localhost:3306\civicrm_uat_logs);

这几乎能工作...

sed -i -e "s/define('CIVICRM_LOGGING_DSN', CIVICRM_DSN);/define('CIVICRM_LOGGING_DSN', 'myuser:mypassword@localhost:3306\civicrm_uat_logs');/" /my_path/civicrm.settings.php

输出是:

define('CIVICRM_LOGGING_DSN', myuser:mypassword@localhost:3306	vicrm_uat_logs);

问题是我缺少'ci'。我认为这是因为反斜杠,但为什么是两个字符呢?!如果我用'my_database'代替civicrm_uat_logs,我只会失去反斜杠。

无论如何,我似乎无法转义反斜杠,但也许还有别的事情在进行?

对这个问题感到困惑,但这应该很简单...

我用的是Ubuntu 22.04 LTS

更新:

我意识到我需要在数据库字符串周围加上引号... 所以替换应该是:

define('CIVICRM_LOGGING_DSN', 'myuser:mypassword@localhost:3306\civicrm_uat_logs');

现在,没有东西被替换,但也没有错误...

英文:

I have been reading through the various sed issues and have one I cannot get to work:

I want to replace:

define('CIVICRM_LOGGING_DSN', CIVICRM_DSN);

With

define('CIVICRM_LOGGING_DSN', myuser:mypassword@localhost:3306\civicrm_uat_logs);

This almost works...

sed -i -e "s/define('CIVICRM_LOGGING_DSN', CIVICRM_DSN);/define('CIVICRM_LOGGING_DSN', 'myuser:mypassword@localhost:3306\civicrm_uat_logs');/" /my_path/civicrm.settings.php

The output is:

define('CIVICRM_LOGGING_DSN', myuser:mypassword@localhost:3306	vicrm_uat_logs);

The issue is I get is that missing 'ci'. This, I assume is due to the backslash, but why two chars?! If I had 'my_database' instead of civicrm_uat_logs , I'd just lose the backslash.

I cannot seem to escape the backslash no matter, but maybe something else is going on?

Struggling with this one, but it must be simple...

I'm on Ubuntu 22.04 LTS

UPDATE:

I realsed that I needed quotes around the database string... so the replacement should be:

define('CIVICRM_LOGGING_DSN', 'myuser:mypassword@localhost:3306\civicrm_uat_logs');

Now,nothing get's replaced but no error...

答案1

得分: 1

好的,借助一位友善而知识丰富的朋友的帮助。Shell 和 sed 是嵌套的,所以:

sed -i -e "s/define('CIVICRM_LOGGING_DSN', CIVICRM_DSN);/define('CIVICRM_LOGGING_DSN', 'civicrm:civicrm@db:3306\\civicrm_uat_logs');/"

转义了反斜杠和 '\c'。

英文:

OK, with the help of a kind and knowledgeable friend. The shell and sed are nested so:

sed -i -e "s/define('CIVICRM_LOGGING_DSN', CIVICRM_DSN);/define('CIVICRM_LOGGING_DSN', 'civicrm:civicrm@db:3306\\\\civicrm_uat_logs');/"

Escaping both the backslash and the '\c'

答案2

得分: -1

使用 `sed`

$ sed -E "s/([^,], )./\1myuser:mypassword@localhost:3306\\civicrm_uat_logs);/" input_file
define('CIVICRM_LOGGING_DSN', myuser:mypassword@localhost:3306\civicrm_uat_logs);

英文:

Using sed

$ sed -E "s/([^,]*, ).*/myuser:mypassword@localhost:3306\\\civicrm_uat_logs);/" input_file
define('CIVICRM_LOGGING_DSN', myuser:mypassword@localhost:3306\civicrm_uat_logs);

huangapple
  • 本文由 发表于 2023年5月10日 18:14:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定