1

I am using sed inside a makefile with the extended regex to replace the text between defined lines with the variable Initial and Final like this -

sed -i -r -e "/$$Initial/,/$$Final/s/$$orig/$$new/g" $$file; \

The above works, but it has lots of forward slash in the values of the variable which works by double escaping those. It would look cleaner if I could replace the delimiter with some other symbols like as follows:

sed -i -r -e "\!$$Initial!,!$$Final!s!$$orig!$$new!g" $$file ; \

But this is showing error with , in the sed expression. Could you please suggest changes?

Porcupine
  • 5,885
  • 2
  • 19
  • 28
  • 2
    typo? the second regex in address range should be `\!$$Final!` – Sundeep Aug 24 '18 at 10:48
  • Hi @Sundeep! Thank you, I really didn't know that I had to put the second backslash. I could not find this from the documentation. Did you refer documentation? – Porcupine Aug 24 '18 at 10:56
  • the rule is `\cregexpc` - see https://stackoverflow.com/questions/5864146/how-to-use-different-delimiters-for-sed-substitute-command – Sundeep Aug 24 '18 at 11:01
  • The regular expression is defined after `s`. So, in my usage, regex is defined by `$$orig`, and we have not used `\!$$orig!` but `!$$orig!`. Isn't it? – Porcupine Aug 24 '18 at 11:10
  • 2
    No, if you want to use a different separator, you have to backslash it. This is so you can say `sed '\!/start/!,\,!/end/!,` to use `!` as the delimiter for the first regex and `,` as the delimiter for the second. These are two separate address expressions which get parsed independently of each other. – tripleee Aug 24 '18 at 11:18

0 Answers0