Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Awk 使用sed查找字符串前后删除行_Awk_Sed - Fatal编程技术网

Awk 使用sed查找字符串前后删除行

Awk 使用sed查找字符串前后删除行,awk,sed,Awk,Sed,下面是我的xml文件内容- <?xml version="1.0" encoding="UTF-8"?> <artifactListing> <folder id="REPORTMART" path="/Repository Objects" pathAlias="/00" modifiedBy="Maria" lastUpdated="1480426973000" description="Hyperion Root Folder"/>

下面是我的xml文件内容-

<?xml version="1.0" encoding="UTF-8"?>
<artifactListing>
    <folder id="REPORTMART" path="/Repository Objects" pathAlias="/00"
        modifiedBy="Maria" lastUpdated="1480426973000" description="Hyperion Root Folder"/>
    <folder id="DATASOURCESFOLD"
        path="/Repository Objects/HRInternalFolder/DataSources"
        pathAlias="/00/HRInternal/DataSources" modifiedBy="Maria" lastUpdated="1492814854000"/>
    <folder id="HRINTERNALFOLD"
        path="/Repository Objects/HRInternalFolder"
        pathAlias="/00/HRInternal" modifiedBy="Maria" lastUpdated="1492814854000"/>
    <folder id="00000158e031595b-0000-0782-0ae57730"
        path="/Repository Objects/TRCS" pathAlias="/00/0"
        modifiedBy="demoadmin" lastUpdated="1492814854000" description="TRCS"/>
    <resource id="JavaScriptUpdateResizeOn_dds_js"
        path="/Repository Objects/Administration/Impact Manager/Script Repository"
        pathAlias="/00/Administration/0/Script_Repository"
        modifiedBy="Maria" lastUpdated="1492814880000"
        description="JavaScript Update DDS configuration with Layout Manager"
        name="JavaScriptUpdateResizeOn_dds.js" type="text/im-javascript" size="-1"/>
    <resource id="449cb46e6b4492f3afb8ef693dffb43a90cdd992"
        path="/Security" pathAlias="/02"
        description="Shared Services Administrator"
        name="epm_default_cloud_admin" type="UserPreferences" size="-1"/>
    <resource id="0f62187cf5a8f5aecec7a9879c9e40497d6d8649"
        path="/Security" pathAlias="/02" description="" name="Jacob"
        type="UserPreferences" size="-1"/>
    <resource id="0df02da8548eeef2174c97c2ade67b4c5adc3160"
        path="/Security" pathAlias="/02" description="" name="Henry"
        type="UserPreferences" size="-1"/>
    <resource id="33dca1c0c1c5ae78f67580a76d9c6aba6a172e20"
        path="/Security" pathAlias="/02" description="" name="Susan"
        type="UserPreferences" size="-1"/>
    <resource id="3e182b1ea9376483a38614d916a0b666ef531b6d"
        path="/Security" pathAlias="/02" description="" name="Maria"
        type="UserPreferences" size="-1"/>
    <resource id="0f62187cf5a8f5aecec7a9879c9e40497d6d8649"
        path="/Security" pathAlias="/02" description="" name="Jacques"
        type="UserPreferences" size="-1"/>
    <resource id="0df02da8548eeef2174c97c2ade67b4c5adc3160"
        path="/Security" pathAlias="/02" description="" name="Frank"
        type="UserPreferences" size="-1"/>
    <resource id="PP_3e182b1ea9376483a38614d916a0b666ef531b6d_0"
        path="/Product Preferences" pathAlias="/05"
        description="This is your default Personal Page."
        name="My Personal Page" type="PersonalPageContent" size="-1"/>
</artifactListing>

现在使用sed,我想删除整个资源标记,如果在其中找到“Susan”字符串,则不应考虑其他非Susan资源标记。
在这个场景中,字符串前后只有一行,在其他情况下,资源标记中有更多的行。

对于您的场景,请尝试以下方法:

$ sed -ibak -r '/^\s*<resource/!bend;:loop;N;/\/>.*$/{/Susan/d;bend};bloop;:end' filename
$sed-ibak-r'/^\s*$/{/Susan/d;bend};bloop;:“结束”文件名
说明:

  • /^\s*$
    ,这意味着我们在
    模式空间
    中获得了一个完整的
    资源
    标记,然后我们可以处理它;如果此完整的
    资源
    标记包含
    Susan
    ,这是您的
    模式
    ,请使用
    d
    命令删除
    模式空间
    中的所有内容,然后跳转到名为
    end
    的标签以启动新循环
  • bloop
    :使用循环将当前
    资源
    标记的剩余行追加到
    模式空间
  • 使用
    -ibak
    备份源文件

  • 注意:它也适用于资源标记中有更多行的其他情况。

    使用XML解析器是处理XML文档的正确方法

    解决方案:

    xmlstarlet ed -d '//resource[@name="Susan"]' yourxmlfile
    

    • ed
      -编辑模式

    • -d
      -删除操作

    • //资源[@name=“Susan”]
      -xpath表达式


    您有能力使用xml解析器吗?更清楚地说,即使在Susan resource标记之前,也有其他用户资源标记,如果我以前错过了,很抱歉。是的,我可以使用xml解析器。尝试使用xmllint,但无法达到我想要的效果。安装xmlstarlet,我将演示如何执行此操作。我没有根目录,将完成此操作并更新您。谢谢,谢谢,魏克。当我运行这个时,sed进入无限循环,当我中断时,我仍然看到文件内容与以前一样。我看到了,这是因为您的xml文件包含的标签比
    资源
    更多。等一下。。。我可以修复它。还要注意,甚至在Susan出现在文件中之前,其他用户的资源标签就更多了。你能粘贴整个xml文件吗?