0

My MWE is here

#!/bin/bash
num=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings | grep -oE '[0-9]+'|sort -rn|head -n 1)
num=$((num + 1))
echo $num
#gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "[<altered_list>]"
#gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "[$key , $key $num]"
key="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom"
list=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
newnum=$(echo "${list}" | grep -oE '[0-9]+' |sort -rn|head -n 1)
echo $newnum
sed -e "/]/${key}${newnum}/" ${list}
#gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "[$key , $key $num]"
echo $key

I want to sed -e "/]/${key}${newnum}/" ${list} that means from -

['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/']   

I want to get

['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/','/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/']

add a extra custom3 in the input.

0

1 Answer 1

0

Use []] to represent the ] in a regex. Use @ as the sed expression delimiter since your replacement string has / in it.

sed -e "s@[]]@${key}${newnum}@" ${list}

You can also escape it with a backslash:

sed -e "s@\]@${key}${newnum}@" ${list}

Since ] is known to be at the end of the string, you could also change the last character without regard to what it is:

sed -e "s@.\$@${key}${newnum}@" ${list}
6
  • sed -e "/\]/${key}${newnum}/" ${list} results sed: -e expression #1, char 5: unknown command: /'`. This error occurs for all 3 expression.
    – alhelal
    Mar 12, 2018 at 5:44
  • You have / in your replacement string that messes up with the sed expression. See the updated answer. Mar 12, 2018 at 5:47
  • same error sed: -e expression #1, char 5: unknown command: @'
    – alhelal
    Mar 12, 2018 at 5:49
  • My bad, s was missing in the beginning. Please see the update. Mar 12, 2018 at 5:51
  • Also note that this sed command assumes that $list is a file while the OP's code seems to want to work on $list itself (as a string) for whatever reason.
    – Kusalananda
    Mar 12, 2018 at 6:32

Not the answer you're looking for? Browse other questions tagged .