Awk 使用sed查找字符串前后删除行
下面是我的xml文件内容-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 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
-xpath表达式//资源[@name=“Susan”]
资源
更多。等一下。。。我可以修复它。还要注意,甚至在Susan出现在文件中之前,其他用户的资源标签就更多了。你能粘贴整个xml文件吗?