You are not logged in.

#1 2022-12-04 13:39:34

kes
Member
Registered: 2017-12-12
Posts: 35

script to swap strings containing newlines within rc.xml

Within my Openbox rc.xml file I have two lines as follows

    <!-- <keybind key="Right"> -->

    <keybind key="s-Up">

I wish to frequently toggle/ swap the above two lines for the below two lines using a script

    <!-- <keybind key="s-Up"> -->

    <keybind key="Right">

This allows me to quickly change a keybinding without tediously editing my rc.xml every time.

The script I have so far is below, and is not working, though I know it is close.

I'm not too bothered how this toggling is achieved.

The sed expressions below work as they should.

And the if statement seems to do the first sed swap if the condition is met but not the second if it is not met.



var_a=""

var_a=$(grep -zoP  "<\!\-\- <keybind key=\"Right\"> \-\->\n\n<keybind key=\"s-Up\">" /home/kes/Dropbox/lubuntu-rc.xml | sed ':a;N;$!ba;s|\n\n||g')

var_a=$(grep -zoP  "<\!\-\- <keybind key=\"Right\"> \-\->\n\n<keybind key=\"s-Up\">" /home/kes/Dropbox/lubuntu-rc.xml | tr -d '\n' )

# result of grep is
# <!-- <keybind key="Right"> --><keybind key="s-Up">

echo $var_a; sleep 0.5

#if [[ -z ! "$var_a" ];then
if [[ '$var_a'=='<!-- <keybind key="Right"> --><keybind key="s-Up">' ]]; then

   sed -ie ':a;N;$!ba;s|<!-- <keybind key="Right"> -->\n\n<keybind key="s-Up">|<!-- <keybind key="s-Up"> -->\n\n<keybind key="Right">|g' /home/kes/Dropbox/lubuntu-rc.xml

else

   sed -ie ':a;N;$!ba;s|<!-- <keybind key="s-Up"> -->\n\n<keybind key="Right">|<!-- <keybind key="Right"> -->\n\n<keybind key="s-Up">|g' /home/kes/Dropbox/lubuntu-rc.xml

fi

Last edited by kes (2022-12-05 09:45:06)

Offline

#2 2022-12-04 15:02:18

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,108
Website

Re: script to swap strings containing newlines within rc.xml

I don't have your lines in my rc.xml so not testing, but here is a full line that should work as an example if it's sed that is the issue.

sed -i 's/<keybind key="s-Up">/<!-- <keybind key="s-Up"> -->/g' "$HOME/Dropbox/lubuntu-rc.xml"

Another thought is creating two files and switching between the two.

Offline

Board footer

Powered by FluxBB