1

I have a file with the following syntax:

slave_master: '1.2.3.4'

and I would like to replace it with sed or awk this way:

slave_master:
  - '1.2.3.4'
  - '1.2.3.5'

The file file is few hundreds lines long and there are other such lines with other IP values which should not be affected. Is it possible to do it with on command? Thanks a lot.

0

what is \1 and \2 in sed ? ( refer this post )

$ cat input.txt
hello: '234.34.34.2'
slave_master: '1.2.3.4'
test: '12.123.123.11'
hi: '234.2.32.33'


$ sed "s/\(slave_master:\)\(.*\)/\1\n     -\2\n     - '1.2.3.5'/" input.txt
hello: '234.34.34.2'
slave_master:
     - '1.2.3.4'
     - '1.2.3.5'
test: '12.123.123.11'
hi: '234.2.32.33'
1

Ok, I found it:

sed -i "s/slave_master: '1.2.3.4'/slave_master:\n \      - '1.2.3.4'\n \      - '1.2.3.5'/g" file.yaml

Note: this should represent the IP of the new nsmaster. I had originally only one nsmaster, then I set up a second one, and had to add it to the yaml file as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.