-2

I already know that sed uses own approach to deal with single quote but I think it still possible to use it in my automation script. I had to replace value of fingerprint in Saltstack config file.

Current value:

#master_finger: ''

Target value

master_finger: 'some:value'

My current command which doesn't work:

$ sed -i 's/#master_finger: ''/master_finger: 'some:value'/g' /etc/salt/minion

returns:

master_finger: some:value''

How can I solve this?

AsterOps
  • 11
  • 3
  • 2
    Does this answer your question? [How to escape single quote in sed?](https://stackoverflow.com/questions/24509214/how-to-escape-single-quote-in-sed) – Wiktor Stribiżew Feb 12 '21 at 20:41
  • 1
    Not exactly. Especially if you look at answer below – AsterOps Feb 12 '21 at 21:40
  • 1
    The answer below says the same: use double quotes ("*Quote sed codes with double quotes*"). This is a 100% duplicate. – Wiktor Stribiżew Feb 12 '21 at 21:42
  • Added here for completeness `<<<"#master_finger: ''" sed -E 's/#(master_finger: ('\''))\2/\1some:value\2/'` N.B. Double quotes allow for the interpolation of the shell which may not always be desired. Whereas single quotes do not. – potong Feb 13 '21 at 14:33
  • with respect to `sed uses own approach to deal with single quote` - no it doesn't. Not sure what you meant by that but single quotes to sed are the same as single quotes to any other Unix tool. Having to write a script that removes `#` in addition to dealing with `'`s is just adding an unnecessary complication to this question. – Ed Morton Feb 13 '21 at 22:17

2 Answers2

0

just use the double quotes to enclose the script.

$ echo "#master_finger: ''" | sed "s/#master_finger: ''/master_finger: 'some:value'/"

master_finger: 'some:value'
karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Single quotes can be managed but it is not pretty `<<<"#master_finger: ''" sed -E 's/#(master_finger: '\'')('\'')/\1some:value\2/'` – potong Feb 13 '21 at 00:41
  • That exposes the script to the shell for globbing, word splitting, variable expansion, command execution, etc. - I'd only enclose a script in double quotes if you **need** that to happen. – Ed Morton Feb 14 '21 at 12:55
0

It's not sed that's making handling of 's difficult, it's the shell because the shell does not allow 's within any '-quoted string, including scripts.

You could save the sed script in a file and run it with -f or use a here document:

$ sed -f- file <<'EOF'
s/#master_finger: ''/master_finger: 'some:value'/g
EOF
master_finger: 'some:value'

To see the difference between the above and @karakfas suggestion:

$ sed -f- file <<'EOF'
s/#master_finger: ''/master_finger: '$(date)'/g
EOF
master_finger: '$(date)'

$ sed "s/#master_finger: ''/master_finger: '$(date)'/" file
master_finger: 'Sun Feb 14 06:50:43 CST 2021'

and imagine if date was replace by rm -rf * or something worse.

Also consider:

$ sed 's/#master_finger: '\'\''/master_finger: '\''$(date)'\''/' file
master_finger: '$(date)'
Ed Morton
  • 188,023
  • 17
  • 78
  • 185