Need to cut a part of a XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to cut a part of a XML file
# 1  
Old 12-09-2008
Need to cut a part of a XML file

I want to be able to search and remove a part of this file. For Example I want to make a search for something like Terminal.app and remove the following from the XML file.

Code:
<dict>
			<key>GUID</key>
			<integer>210535539</integer>
			<key>tile-data</key>
			<dict>
				<key>dock-extra</key>
				<false/>
				<key>file-data</key>
				<dict>
					<key>_CFURLAliasData</key>
					<data>
					AAAAAAC0AAMAAQAAw+NQpgAASCsAAAAAAAAA
					gAAATbQAAMOiInsAAAAACSD//gAAAAAAAAAA
					/////wABAAgAAACAAAAAfwAOABoADABUAGUA
					cgBtAGkAbgBhAGwALgBhAHAAcAAPABoADABN
					AGEAYwBpAG4AdABvAHMAaAAgAEgARAASACNB
					cHBsaWNhdGlvbnMvVXRpbGl0aWVzL1Rlcm1p
					bmFsLmFwcAAAEwABLwD//wAA
					</data>
					<key>_CFURLString</key>
					<string>/Applications/Utilities/Terminal.app</string>
					<key>_CFURLStringType</key>
					<integer>0</integer>
				</dict>
				<key>file-label</key>
				<string>Terminal</string>
				<key>file-mod-date</key>
				<integer>3298318198</integer>
				<key>file-type</key>
				<integer>41</integer>
				<key>parent-mod-date</key>
				<integer>3304372748</integer>
			</dict>
			<key>tile-type</key>
			<string>file-tile</string>
		</dict>

From this HUGE Plist.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>mod-count</key>
	<integer>8</integer>
	<key>persistent-apps</key>
	<array>
		<dict>
			<key>GUID</key>
			<integer>210535539</integer>
			<key>tile-data</key>
			<dict>
				<key>dock-extra</key>
				<false/>
				<key>file-data</key>
				<dict>
					<key>_CFURLAliasData</key>
					<data>
					AAAAAAC0AAMAAQAAw+NQpgAASCsAAAAAAAAA
					gAAATbQAAMOiInsAAAAACSD//gAAAAAAAAAA
					/////wABAAgAAACAAAAAfwAOABoADABUAGUA
					cgBtAGkAbgBhAGwALgBhAHAAcAAPABoADABN
					AGEAYwBpAG4AdABvAHMAaAAgAEgARAASACNB
					cHBsaWNhdGlvbnMvVXRpbGl0aWVzL1Rlcm1p
					bmFsLmFwcAAAEwABLwD//wAA
					</data>
					<key>_CFURLString</key>
					<string>/Applications/Utilities/Terminal.app</string>
					<key>_CFURLStringType</key>
					<integer>0</integer>
				</dict>
				<key>file-label</key>
				<string>Terminal</string>
				<key>file-mod-date</key>
				<integer>3298318198</integer>
				<key>file-type</key>
				<integer>41</integer>
				<key>parent-mod-date</key>
				<integer>3304372748</integer>
			</dict>
			<key>tile-type</key>
			<string>file-tile</string>
		</dict>
		<dict>
			<key>GUID</key>
			<integer>1059712049</integer>
			<key>tile-data</key>
			<dict>
				<key>dock-extra</key>
				<false/>
				<key>file-data</key>
				<dict>
					<key>_CFURLAliasData</key>
					<data>
					AAAAAACyAAMAAQAAw+NQpgAASCsAAAAAAAAA
					fwAAcoEAAMOzM4YAAAAACSD//gAAAAAAAAAA
					/////wABAAQAAAB/AA4AIgAQAFQAaQBtAGUA
					IABNAGEAYwBoAGkAbgBlAC4AYQBwAHAADwAa
					AAwATQBhAGMAaQBuAHQAbwBzAGgAIABIAEQA
					EgAdQXBwbGljYXRpb25zL1RpbWUgTWFjaGlu
					ZS5hcHAAABMAAS8A//8AAA==
					</data>
					<key>_CFURLString</key>
					<string>/Applications/Time Machine.app/</string>
					<key>_CFURLStringType</key>
					<integer>0</integer>
				</dict>
				<key>file-label</key>
				<string>Time Machine</string>
				<key>file-mod-date</key>
				<integer>3298318198</integer>
				<key>file-type</key>
				<integer>169</integer>
				<key>parent-mod-date</key>
				<integer>3311538379</integer>
			</dict>
			<key>tile-type</key>
			<string>file-tile</string>
		</dict>
	</array>
	<key>persistent-others</key>
	<array/>
	<key>trash-full</key>
	<false/>
	<key>version</key>
	<integer>1</integer>
</dict>
</plist>

# 2  
Old 12-09-2008
Kindly make it clear. Which tag (with path) you need to search and which block need to remove
# 3  
Old 12-09-2008
Your query is somewhat vague but I think what you are asking for is a method to selectively remove entire application nodesets from a document. If this is the case, here is a stylesheet which should do what you want to do.
Code:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:param name="application">Time Machine</xsl:param>

  <xsl:output method="xml"/>

  <xsl:template match="node() | @*">
     <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
     </xsl:copy>
  </xsl:template>

  <xsl:template match="/plist/dict/array/dict">
     <xsl:if test="not(contains(.//string,$application))">
        <xsl:copy-of select="." />
     </xsl:if>
  </xsl:template>

</xsl:stylesheet>

Assuming you use xsltproc as your stylesheet transformation engine, xslfile is your stylesheet, xmlfile is your input document and you want to remove the "Terminal.asp" node-set, use the following command to transform the document. Note the use of Java style strings. If you do not pass in an application name, the Time Machine application nodeset is removed by default.
Code:
$ xsltproc -param application "'Terminal'" xslfile xmlfile

The following output will be produced
Code:
<?xml version="1.0"?>
<plist version="1.0">
<dict>
        <key>mod-count</key>
        <integer>8</integer>
        <key>persistent-apps</key>
        <array>
                <dict>
                        <key>GUID</key>
                        <integer>1059712049</integer>
                        <key>tile-data</key>
                        <dict>
                                <key>dock-extra</key>
                                <false/>
                                <key>file-data</key>
                                <dict>
                                        <key>_CFURLAliasData</key>
                                        <data>
                                        AAAAAACyAAMAAQAAw+NQpgAASCsAAAAAAAAA
                                        fwAAcoEAAMOzM4YAAAAACSD//gAAAAAAAAAA
                                        /////wABAAQAAAB/AA4AIgAQAFQAaQBtAGUA
                                        IABNAGEAYwBoAGkAbgBlAC4AYQBwAHAADwAa
                                        AAwATQBhAGMAaQBuAHQAbwBzAGgAIABIAEQA
                                        EgAdQXBwbGljYXRpb25zL1RpbWUgTWFjaGlu
                                        ZS5hcHAAABMAAS8A//8AAA==
                                        </data>
                                        <key>_CFURLString</key>
                                        <string>/Applications/Time Machine.app/</string>
                                        <key>_CFURLStringType</key>
                                        <integer>0</integer>
                                </dict>
                                <key>file-label</key>
                                <string>Time Machine</string>
                                <key>file-mod-date</key>
                                <integer>3298318198</integer>
                                <key>file-type</key>
                                <integer>169</integer>
                                <key>parent-mod-date</key>
                                <integer>3311538379</integer>
                        </dict>
                        <key>tile-type</key>
                        <string>file-tile</string>
                </dict>
        </array>
        <key>persistent-others</key>
        <array/>
        <key>trash-full</key>
        <false/>
        <key>version</key>
        <integer>1</integer>
</dict>
</plist>

# 4  
Old 12-09-2008
I will try it and report back..thanks
# 5  
Old 12-10-2008
Sorry but I think I'm more confused than before.

The Entire string I need to remove is about 36 Lines.

Code:
<dict>
			<key>GUID</key>
			<integer>210535539</integer>
			<key>tile-data</key>
			<dict>
				<key>dock-extra</key>
				<false/>
				<key>file-data</key>
				<dict>
					<key>_CFURLAliasData</key>
					<data>
					AAAAAAC0AAMAAQAAw+NQpgAASCsAAAAAAAAA
					gAAATbQAAMOiInsAAAAACSD//gAAAAAAAAAA
					/////wABAAgAAACAAAAAfwAOABoADABUAGUA
					cgBtAGkAbgBhAGwALgBhAHAAcAAPABoADABN
					AGEAYwBpAG4AdABvAHMAaAAgAEgARAASACNB
					cHBsaWNhdGlvbnMvVXRpbGl0aWVzL1Rlcm1p
					bmFsLmFwcAAAEwABLwD//wAA
					</data>
					<key>_CFURLString</key>
					<string>/Applications/Utilities/Terminal.app</string>
					<key>_CFURLStringType</key>
					<integer>0</integer>
				</dict>
				<key>file-label</key>
				<string>Terminal</string>
				<key>file-mod-date</key>
				<integer>3298318198</integer>
				<key>file-type</key>
				<integer>41</integer>
				<key>parent-mod-date</key>
				<integer>3304372748</integer>
			</dict>
			<key>tile-type</key>
			<string>file-tile</string>
</dict>

I want to be able to search for the File Label Key and Remove the entire 36 lines.
Code:
<key>file-label</key>
<string>Terminal</string>

# 6  
Old 12-10-2008
Actually I got a chance to read your thread 20 times and not sure how but it works. Well I have a general Idea. Thanks.
# 7  
Old 12-10-2008
Glad it worked for you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to cut part of a string in reverse?

Hi, how to cut part of a string sing delimiter in reverse input file 1,2,st-pa-tr-01,2,3,4, 2,3,ff-ht-05,6,7,8 how can i obtain strings till st-pa-tr ff-ht i.e cutting the last part og string -01 and -05 Thanks & Regards Nivi edit by bakunin: changed thread title (typo) (3 Replies)
Discussion started by: nivI
3 Replies

2. Shell Programming and Scripting

cut the some part in filename

Hi All, I have the file & name is "/a/b/c/d/e/xyz.dat" I need "/a/b/c/d/e/" from the above file name. I tryning with echo and awk. But it not come. Please help me in this regard. Thanks & Regards, Dathu (3 Replies)
Discussion started by: pdathu
3 Replies

3. UNIX for Dummies Questions & Answers

cut and print part of a string

I have a file that contains: yahoo.com.23456 web.log.common.us.gov.8675 192.168.1.55.34443 john-doe.about.com.22233 64.222.3.4.120 sunny.ca.4442 how can i remove the strings after the last dot (.) and reprint the file? Thanks. (3 Replies)
Discussion started by: apalex
3 Replies

4. Shell Programming and Scripting

Cut xml value with in a tag

Hi, How can I cut/copy or assign it to a variable of a value in xml tag. for example: below is the xml tag in an xml <description>Successfully processed the request</description> now I need a ouput like below. my $description = /<description>($.)<\/description>/; print... (1 Reply)
Discussion started by: thankful123
1 Replies

5. UNIX for Dummies Questions & Answers

How to cut a string in two parts and show the other part

hi everybody.. I have a string like : abcd:efgh xxyy:yyxx ssddf:kjlioi ghtyu:jkksk nhjkk:heuiiue please tell me how i can display only the characters after ":" in the output the output should be : efgh yyxx kjlioi jkksk heuiiue please give quick reply.. its urgent..!! (6 Replies)
Discussion started by: adityamitra
6 Replies

6. UNIX for Dummies Questions & Answers

Help me with cut part 2..

Hi, I have this file name : xxx.77876767575.abc.77887.iiii If to get only the xxx i will need to do this command: i=xxx.77876767575.abc.77887.iiii name=`echo $i |cut -f1 -d "."` How do i get 77876767575.abc.77887.iiii without xxx in front? Please advice. Thanks (7 Replies)
Discussion started by: luna_soleil
7 Replies

7. Shell Programming and Scripting

Extracting a part of XML File

Hi Guys, I have a very large XML feed (2.7 MB) which crashes the server at the time of parsing. Now to reduce the load on the server I have a cron job running every 5 min.'s. This job will get the file from the feed host and keep it in the local machine. This does not solve the problem as... (9 Replies)
Discussion started by: shridhard
9 Replies

8. Shell Programming and Scripting

cut a part of the file

This is a part of the output file (x.out, from a chemical software). I need to extract the xyz coordinates with the atom labels from this file.If it's possible i would like to use a bash shell script. I am new to this forum, how can I solve this problem. DIPOLE X Y Z ... (1 Reply)
Discussion started by: kurosaki
1 Replies

9. Shell Programming and Scripting

cut part of line

Dear Sirs, I want to cut the IP address from the following text line: " mgmt.mib-2.bgp.bgpPeerTable.bgpPeerEntry.bgpPeerLastError.163.121.170.20 (OctetString): 0x04 00" I want to get the (163.121.170.20) only. Thanks in advance. (8 Replies)
Discussion started by: ahmed.zaher
8 Replies

10. Shell Programming and Scripting

using sed with xml files part 2

I'm trying to replace a date in an XML file that has the format mm/dd/yyyy. I'm using the Unix date function to set up a variable with the current date but, when I try to replace the value in the XML file, the error message says it cannot be parsed. Here is the command I'm using ... (2 Replies)
Discussion started by: stonemonolith
2 Replies
Login or Register to Ask a Question