0

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...

ChumKui
  • 211
  • 1
  • 2
  • 9
  • 1
    Double the backslash in the replacement. – Wiktor Stribiżew May 10 '23 at 10:19
  • `\c` [seems to suppress output](https://stackoverflow.com/questions/7154800/what-is-the-bash-escape-character-c) of the following symbols. – markalex May 10 '23 at 10:21
  • \c does something weird and when I add the quotes in, I get no replacement at all. I cannot seem to escape the \c... – ChumKui May 10 '23 at 11:40
  • See [is-it-possible-to-escape-regex-metacharacters-reliably-with-sed](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed) as it covers this case and others you might run into if you want to use sed for this (instead of awk or some other tool that understands literal strings). – Ed Morton May 10 '23 at 12:15

2 Answers2

1

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'

ChumKui
  • 211
  • 1
  • 2
  • 9
-1

Using sed

$ sed -E "s/([^,]*, ).*/\1myuser:mypassword@localhost:3306\\\civicrm_uat_logs);/" input_file
define('CIVICRM_LOGGING_DSN', myuser:mypassword@localhost:3306\civicrm_uat_logs);
HatLess
  • 10,622
  • 5
  • 14
  • 32