Skip to content
Advertisement

Sed with special caracters

How can I replace this with sed ? I need to replace this:

JavaScript

to

JavaScript

Please note that I can’t sed the enabled to disabled because it’s not only used at this location in the input file.

I tried this but it didn’t change anything but gave me no error:

JavaScript

Advertisement

Answer

You can just use the following sed command:

CMD:

JavaScript

Explanations:

  • Just use double quote and add the $ in a class character group to avoid that your shell interprets $protection as a variable
  • If you need to modify a file change your command into: sed -i.back "s/set [$]protection 'enabled';/set $protection 'disabled';/g" it will take a backup of your file and do the modifications in-place.
  • Also you can add starting ^ and closing $ anchors to your regex if there is nothing else on the lines you want to change. ^set [$]protection 'enabled';$

INPUT:

JavaScript

OUTPUT:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement