0

The last few lines of my file /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml look like this:

    <key name='notify-on-connect' type='b'>
      <summary>Notify on connect</summary>
      <description>
        If true, show a notification when a user connects to the system.
      </description>
      <default>true</default>
    </key>
  </schema>
</schemalist>

Now I want to add a few lines before the line to achieve the following effect:

    <key name='notify-on-connect' type='b'>
      <summary>Notify on connect</summary>
      <description>
        If true, show a notification when a user connects to the system.
      </description>
      <default>true</default>
    </key>

    <key name='enabled' type='b'>
      <summary>Enable remote access to the desktop</summary>
      <description>
      If true, allows remote access to the desktop via the RFB
      protocol. Users on remote machines may then connect to the
      desktop using a VNC viewer.
      </description>
      <default>false</default>
    </key>
  </schema>
</schemalist>

I now need to write a shell script to achieve the above effect via the sed command, which I have written as shown below:

sed -i "/\</schema\>/i  \    <key name='enabled' type='b'>\n      <summary>Enable remote access to the desktop</summary>\n      <description>\n        If true, allows remote access to the desktop via the RFB\n        protocol. Users on remote machines may then connect to the\n        desktop using a VNC viewer.\n      </description>\n      <default>false</default>\n    </key>\n" /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml

But after the execution, the content is not added. How can I use sed to write it to achieve my effect above? By the way: the line </schema> is preceded by two spaces.

1 Answer 1

0

The i\ command takes literal hard-coded newlines escaped by a backslash rather than \n Use the following template to make changes in the input file.

sed -i.BAK -e '
/<\/schema>/i\
    <key name='\''enabled'\'' type='\''b'\''>\
      <summary>Enable remote access to the desktop</summary>\
      <description>\
      If true, allows remote access to the desktop via the RFB\
      protocol. Users on remote machines may then connect to the\
      desktop using a VNC viewer.\
      </description>\
      <default>false</default>\
    </key>
' /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml

## now do a diff between these:
diff /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml.BAK
3
  • thanks very much!
    – user472795
    Commented May 21, 2021 at 2:55
  • Using your method, I have added the content and now I want to find the content in the file by using the grep command, How do I find out what was added to that paragraph above by using the grep command
    – user472795
    Commented May 21, 2021 at 3:03
  • Do a diff between the original and the modified. But first save the original into some backup. Or, give an extension to the -i option. See my edits for the procedure.
    – guest_7
    Commented May 21, 2021 at 4:15

You must log in to answer this question.

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