33

I am trying to use this command:

sed -i 's#\{test1\}#test2#' /example/myfile.txt

To replace instances of {test1} with test2.

I get the error:

sed: -e expression #1, char 17: Invalid preceding regular expression

Am I not escaping the curly braces correctly?

ericsoco
  • 24,913
  • 29
  • 97
  • 127
atdev
  • 479
  • 1
  • 5
  • 9

2 Answers2

48
sed -i 's#{test1}#test2#' /example/myfile.txt

You don't need escape {}

kev
  • 155,172
  • 47
  • 273
  • 272
28

You aren't escaping the curly braces at all. In sed, the default regular expressions are BREs, where \{ and \} indicate a range expression. Since test1 isn't a range, your BRE is incorrect.

To fix it, you can either drop the backslashes (braces aren't special in BREs) or keep it the same and tell sed to use EREs (-r flag with GNU sed, -E flag with BSD/MacOSX sed).

Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88