grep / sed / awk / 计数文件中两行之间的数量而不是整个文件

grep / sed / awk / 计数文件中两行之间的数量而不是整个文件

我的笔记本电脑上有这个文件/etc/dbus-1/system.d/org.freedesktop.thermald.conf

<?xml version="1.0"?> <!--*-nxml-*-->
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
        "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">

<!--
  This file is part of systemd.

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
-->

<busconfig>

        <policy user="root">
                <allow own="org.freedesktop.thermald"/>
                <allow send_destination="org.freedesktop.thermald"/>
                <allow receive_sender="org.freedesktop.thermald"/>
        </policy>

        <policy context="default">
                <deny send_destination="org.freedesktop.thermald"/>
                <allow receive_sender="org.freedesktop.thermald"/>
        </policy>

</busconfig>

我需要知道:

<allow send_destination="org.freedesktop.thermald"/>

但前提是不低于 root必须符合 default

搜索文件必须只能介于:

    <policy context="default">

下一行包含:

    </policy>

我正在寻找最优雅(且易读)的方式来实现此目的,而不偏向grepsedawk任何其他常见实用程序。

这是显示 DBus 的可扩展/可折叠 tkinter 树形视图的 Python 程序的一部分。我正在捕获无法自省的系统总线上的权限错误。自省和拒绝已经成功捕获,但捕获太多。因为它是 Python,所以jsonxmltree模块已经在使用,并且 Python 内置listdict函数可用。

答案1

安装xmllint

apt-get install libxml2-utils

检查上下文中是否存在 xml 路径default

xmllint --xpath "//busconfig/policy[@context='default']/allow[@send_destination='org.freedesktop.thermald']" /etc/dbus-1/system.d/org.freedesktop.thermald.conf >/dev/null 2>&1 && echo good || echo bad

如果您的路径在其他上下文中不存在,则反之亦然:

! xmllint --xpath "//busconfig/policy[@user='root']/allow[@send_destination='org.freedesktop.thermald']" /etc/dbus-1/system.d/org.freedesktop.thermald.conf >/dev/null 2>&1 && echo good || echo bad

答案2

使用sedgrepawk(但不能同时使用):

sed -n '/<policy context="default">/,/<\/policy>/p' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<allow send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"

或者

awk '/<policy context="default">/,/<\/policy>/' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<allow send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"

例子:

正在搜索allow send_destination

$ sed -n '/<policy context="default">/,/<\/policy>/p' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<allow send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"
Not Found

正在搜索deny send_destination

$ sed -n '/<policy context="default">/,/<\/policy>/p' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<deny send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"
                <deny send_destination="org.freedesktop.thermald"/>

awk工作原理几乎相同sed

$ awk '/<policy context="default">/,/<\/policy>/' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<allow send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"
Not Found

$ awk '/<policy context="default">/,/<\/policy>/' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<deny send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"
                <deny send_destination="org.freedesktop.thermald"/>

相关内容