Reputation: 45
below is my xml file content -
<?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>
now using sed I would like to delete entire resource tag if "Susan" string is found within it, other non-Susan resource tag's should not be considered. In this scenario, it's only 1 line before and after string, I've other cases where there are more line within the resource tag.
Upvotes: 1
Views: 129
Reputation: 92854
Using XML parser is the right way for manipulating XML documents.
xmlstarlet solution:
xmlstarlet ed -d '//resource[@name="Susan"]' yourxmlfile
ed
- edit mode
-d
- delete action
//resource[@name="Susan"]
- xpath expression
Upvotes: 1
Reputation: 1270
For your scenario, try this:
$ sed -ibak -r '/^\s*<resource/!bend;:loop;N;/\/>.*$/{/Susan/d;bend};bloop;:end' filename
Explains:
/^\s*<resource/!bend
: if pattern space
does NOT start with ^\s*<resource
, jump to the label named end
to start a new loop.:loop
: set a label named loop
to deal with the whole resource
tag.N
: use N
command to append a \n
and next line
into pattern space
./\/>.*$/{/Susan/d}
: if current pattern space
ends with />$
, which means we have got a complete resource
tag in pattern space
, then we can deal with it; if this complete resource
tag contains Susan
, which is your pattern
, use d
command to delete all contents in pattern space
and then jump to label named end
to start a new loop.bloop
: use a loop to append the remaining lines of current resource
tag into pattern space
.-ibak
to backup the origin file.P.S: It will ALSO work for other cases where there are more lines within the resource tag.
Upvotes: 0