sed find and replace with curly braces

后端 未结 2 628
野趣味
野趣味 2020-12-30 22:59

I am trying to use this command:

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

To replace instances of {test1} with

2条回答
  •  说谎
    说谎 (楼主)
    2020-12-30 23:25

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

提交回复
热议问题