免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6924 | 回复: 1
打印 上一主题 下一主题

[文本处理] 如何使用shell脚本在文本中的特定位置添加内容? [复制链接]

论坛徽章:
5
程序设计版块每日发帖之星
日期:2016-04-15 06:20:00每日论坛发贴之星
日期:2016-04-15 06:20:0015-16赛季CBA联赛之八一
日期:2016-07-08 09:20:28操作系统版块每日发帖之星
日期:2016-08-03 06:20:002016科比退役纪念章
日期:2016-10-30 13:59:12
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2021-05-03 23:01 |只看该作者 |倒序浏览
在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个空格。我应该如何写脚本达到这种效果。

论坛徽章:
25
程序设计版块每日发帖之星
日期:2016-05-03 06:20:0015-16赛季CBA联赛之八一
日期:2018-07-05 10:34:09黑曼巴
日期:2018-07-06 15:19:5015-16赛季CBA联赛之佛山
日期:2018-08-03 13:19:3315-16赛季CBA联赛之山西
日期:2018-08-07 19:46:2315-16赛季CBA联赛之广夏
日期:2018-08-08 19:31:5015-16赛季CBA联赛之青岛
日期:2018-11-26 15:21:5015-16赛季CBA联赛之上海
日期:2018-12-11 09:45:3219周年集字徽章-年
日期:2020-04-18 23:54:5215-16赛季CBA联赛之深圳
日期:2020-04-19 21:40:19黑曼巴
日期:2022-04-03 17:55:1315-16赛季CBA联赛之八一
日期:2018-07-03 16:56:46
2 [报告]
发表于 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>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP