0

Why does this sed does not work?

sed 's/DIRS = \[\]/ DIRS = \[os.path.join\(BASE_DIR, \'templates\',\'allauth\',\'account\'\)\]/' settings.py

running this just gives a ">" prompt (as if something is unterminated).

user1510223
  • 11
  • 2
  • 6
  • 1
    https://stackoverflow.com/questions/4052253/how-do-you-debug-a-regular-expression-with-sed – matt Aug 04 '19 at 03:44
  • 1
    [How to escape single quotes within single quoted strings?](https://stackoverflow.com/q/1250079/3266847) – Benjamin W. Aug 04 '19 at 04:01

1 Answers1

0

Its because of your usage with and ().

Although I don’t underdstand why have you complicated the expression so much, just use:

sed "s/DIRS = \[\]/ DIRS = [os.path.join(BASE_DIR, 'templates','allauth','account')]/" settings.py
Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39
  • 1
    Notice that this relies on the shell not removing ``\`` within double quotes when followed by `[`. The safe way (that doesn't require knowing which characters following ``\`` leave it unchanged) would be `\\[\\]`. – Benjamin W. Aug 04 '19 at 04:08
  • @BenjaminW., I am not sure about this, doesn’t he want to replace `DIRS = []` so he escaped them `[` and `]`? – Mihir Luthra Aug 04 '19 at 09:45
  • Yes, but within double quotes, shell escaping takes place before sed gets to see your string, and certain characters following the ``\`` make it disappear - `[` and `]` happen to not be among them, though. – Benjamin W. Aug 04 '19 at 16:51