Search code examples
stringsedreplaceyamldouble-quotes

How to use sed to replace a string within a file with a another string surrounded by single quotes?


I have yaml file with content:

dummy:
  key1: value1
  key2: value2

I want to use sed to replace dummy with 'dummy'

I am trying:

sed -i '.yaml' 's/dummy/"'dummy'"/g' ~/Desktop/test.yaml

I get

"dummy":
  key1: value1
  key2: value2

I am getting a dummy string surrounded by double quotes as you can see...

IN FACT I want single quotes surrounding dummy

How do I get? :

'dummy':
  key1: value1
  key2: value2

Solution

  • EDIT: As per OP adding solution too now.

    sed "s#dummy#12345#"  Input_file
    

    Following simple sed may help you here.

    sed "s#dummy#'&'#"  Input_file
    

    In case you want to save changes into Input_file itself by taking backup of Input_file then use following.

    sed  -i.bak "s#dummy#'&'#"  Input_file
    

    In awk:

    awk '{sub("dummy","\047&\047")} 1'  Input_file