Chinaunix

标题: 如何使用shell脚本在文本中的特定位置添加内容? [打印本页]

作者: 274920831    时间: 2021-05-03 23:01
标题: 如何使用shell脚本在文本中的特定位置添加内容?
在ubuntu 18.04系统中,我查看这个命令:
cat /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml
最后一部分的内容如下所示:


    <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>




现在我想在倒数第三行开始插入一段内容,插入后如下所示:

    <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>



上面红色是我插入的内容,请问我如果要实现这种插入,shell脚本应该如何来写?在指定的位置插入一段内容,并且这一段内容,每一行前面的空格也是固定的,
像上图中红框中的第一行  <key name=’enabled’ type=’b’>,前面有4个空格。而第二行 <summary>Enable remote access to the desktop</summary>,前面有6个空格。我应该如何写脚本达到这种效果。

作者: wh7211    时间: 2021-05-27 17:14
回复 1# 274920831


  1. cat 1
  2.     <key name='notify-on-connect' type='b'>
  3.       <summary>Notify on connect</summary>
  4.       <description>
  5.         If true, show a notification when a user connects to the system.
  6.       </description>
  7.       <default>true</default>
  8.     </key>
  9.   </schema>
  10. </schemalist>

  11. cat 2
  12.     <key name=’enabled’ type=’b’>
  13.       <summary>Enable remote access to the desktop</summary>
  14.       <description>
  15.         If true, allows remote access to the desktop via the RFB
  16.         protocol. Users on remote machines may then connect to the
  17.         desktop using a VNC viewer.
  18.       </description>
  19.       <default>false</default>
  20.     </key>

  21. awk 'FILENAME==ARGV[1]{a=a?a"\n"$0:$0;next}/^  <\/schema>$/{printf("\n%s\n\n",a)}1' 2 1
  22.     <key name='notify-on-connect' type='b'>
  23.       <summary>Notify on connect</summary>
  24.       <description>
  25.         If true, show a notification when a user connects to the system.
  26.       </description>
  27.       <default>true</default>
  28.     </key>

  29.     <key name=’enabled’ type=’b’>
  30.       <summary>Enable remote access to the desktop</summary>
  31.       <description>
  32.         If true, allows remote access to the desktop via the RFB
  33.         protocol. Users on remote machines may then connect to the
  34.         desktop using a VNC viewer.
  35.       </description>
  36.       <default>false</default>
  37.     </key>

  38.   </schema>
  39. </schemalist>
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2