Rewriting file paths in XML file within bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rewriting file paths in XML file within bash script
# 1  
Old 04-09-2010
Rewriting file paths in XML file within bash script

Hi guys,

I'm working on a large set of scripts to move files around several servers and manipulate them for our staff. Basically we're shooting things, the videos hit a server and then need organised due to the language they've been shot in. Our XML (designed for Apple's Final Cut Pro) is right obviously when captured but not when it hits the edit stations.

This is the original XML (a snippet of):

Code:
<file id="DE_TEST_SL01.mov">
	<name>DE_TEST_SL01.mov</name>
	<pathurl>file:///Volumes/White%20Stage/2%20Stage%20capture/DE_TEST_SL01.mov</pathurl>
</file>

We need to rewrite this as such:

Code:
<file id="DE_TEST_SL01.mov">
	<name>DE_TEST_SL01.mov</name>
	<pathurl>file:///Volumes/ctu/DE/4.\ Final\ Cut\ Pro\ Documents/Capture\ Scratch/DE_TEST_SL01.mov</pathurl>
</file>

Can this be done simply with a one or two line script?

I'd rather batch rewrite so any files named DE_.something.xml would all be written to that capture scratch in the DE folder.


Thanks in advance Smilie


Edit: Just realised that I've not mentioned why I'm posting this specifically - because of the slashes present in file paths I'm not sure how to do this. Smilie

Last edited by omfgbunnies; 04-09-2010 at 09:58 AM..
# 2  
Old 04-09-2010
Does the path mentioned in output is always the same?

Code:
 
Volumes/ctu/DE/4.\ Final\ Cut\ Pro\ Documents/Capture\ Scratch

# 3  
Old 04-09-2010
No - the path mentioned changes based on language - Spanish, German or French (ES, DE, FR). Each file starts with ES_, DE_ or FR_.

e.g.

Code:
/Volumes/ctu/DE
/Volumes/ctu/ES
/Volumes/ctu/FR

# 4  
Old 04-09-2010
Try this
Code:
$ echo '<file id="DE_TEST_SL01.mov">
<name>DE_TEST_SL01.mov</name>
<pathurl>file:///Volumes/White%20Stage/2%20Stage%20capture/DE_TEST_SL01.mov</pathurl>
</file>' | sed 's!White%20Stage/2%20Stage%20capture!ctu/DE/4.\\ Final\\ ut\\ Pro\\ Documents/Capture\\ Scratch!;s!%20!\\ !g'

# 5  
Old 04-09-2010
Thanks for this! It's great to see that it replaces the string of text.

I'm almost completely happy but am being a little confuzzled by how I get the string replaced inside the entire XML document.

Because of my lack of experience I'd probably attempt some stupid method!

XML quoted in entirety for completeness. Smilie

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xmeml>
<xmeml version="4">
<importoptions>
<createnewproject>FALSE</createnewproject>
<displaynonfatalerrors>FALSE</displaynonfatalerrors>
<defsequencepresetname>NO PRESET</defsequencepresetname>
</importoptions>
<clip id="DE_TEST_SL06_TK01-00h04m32s02">
	<name>DE_TEST_SL06_TK01-00h04m32s02</name>
	<duration>43248</duration>
	<rate>
		<ntsc>FALSE</ntsc>
		<timebase>24</timebase>
	</rate>
	<in>77</in>
	<out>413</out>
	<anamorphic>FALSE</anamorphic>
	<masterclipid>DE_TEST_SL06_TK01</masterclipid>
	<ismasterclip>TRUE</ismasterclip>
	<logginginfo>
		<lognote> </lognote>
	</logginginfo>
	<labels>
		<label> </label>
	</labels>
	<media>
		<video>
			<track>
				<clipitem>
					<name>DE_TEST_SL01.mov</name>
					<duration>43248</duration>
					<in>0</in>
					<out>43248</out>
					<start>0</start>
					<end>43248</end>
					<enabled>TRUE</enabled>
					<anamorphic>FALSE</anamorphic>
					<masterclipid>DE_TEST_SL06_TK01</masterclipid>
					<logginginfo>
						<lognote> </lognote>
					</logginginfo>
					<labels>
						<label> </label>
					</labels>
					<file id="DE_TEST_SL01.mov">
						<name>DE_TEST_SL01.mov</name>
						<pathurl>file:///Volumes/White%20Stage/2%20Stage%20capture/DE_TEST_SL01.mov</pathurl>
						<timecode>
							<string>00:04:28:22</string>
							<frame>6454</frame>
							<displayformat>NDF</displayformat>
							<source>source</source>
							<reel>
								<name>PictureReady</name>
							</reel>
						</timecode>
						<rate>
							<timebase>24</timebase>
						</rate>
						<duration>413</duration>
						<media>
							<video>
								<duration>413</duration>
							</video>
							<audio>
								<trackcount>2</trackcount>
							</audio>
						</media>
					</file>
					<sourcetrack>
						<mediatype>video</mediatype>
					</sourcetrack>
					<link>
						<mediatype>video</mediatype>
						<trackindex>1</trackindex>
						<clipindex>1</clipindex>
					</link>
					<link>
						<mediatype>audio</mediatype>
						<trackindex>1</trackindex>
						<clipindex>1</clipindex>
					</link>
					<link>
						<mediatype>audio</mediatype>
						<trackindex>2</trackindex>
						<clipindex>1</clipindex>
					</link>
				</clipitem>
				<enabled>TRUE</enabled>
			</track>
		</video>
		<audio>
			<in>77</in>
			<out>413</out>
			<track>
				<clipitem>
					<name>DE_TEST_SL01.mov</name>
					<duration>413</duration>
					<in>0 </in>
					<out>43248</out>
					<start>0</start>
					<end>43248</end>
					<subframeoffset>0</subframeoffset>
					<enabled>TRUE</enabled>
					<masterclipid>DE_TEST_SL06_TK01</masterclipid>
					<file id="DE_TEST_SL01.mov"/>
					<sourcetrack>
						<mediatype>audio</mediatype>
						<trackindex>1</trackindex>
					</sourcetrack>
					<link>
						<mediatype>video</mediatype>
						<trackindex>1</trackindex>
						<clipindex>1</clipindex>
					</link>
					<link>
						<mediatype>audio</mediatype>
						<trackindex>1</trackindex>
						<clipindex>1</clipindex>
					</link>
					<link>
						<mediatype>audio</mediatype>
						<trackindex>2</trackindex>
						<clipindex>1</clipindex>
					</link>
				</clipitem>
				<enabled>TRUE</enabled>
			</track>
			<track>
				<clipitem>
					<name> DE_TEST_SL01.mov</name>
					<duration>413</duration>
					<in>0</in>
					<out>43248</out>
					<start>0</start>
					<end>43248</end>
					<subframeoffset>0</subframeoffset>
					<enabled>TRUE</enabled>
					<masterclipid>DE_TEST_SL06_TK01</masterclipid>
					<file id="DE_TEST_SL01.mov"/>
					<sourcetrack>
						<mediatype>audio</mediatype>
						<trackindex>2</trackindex>
					</sourcetrack>
					<link>
						<mediatype>video</mediatype>
						<trackindex>1</trackindex>
						<clipindex>1</clipindex>
					</link>
					<link>
						<mediatype>audio</mediatype>
						<trackindex>1</trackindex>
						<clipindex>1</clipindex>
					</link>
					<link>
						<mediatype>audio</mediatype>
						<trackindex>2</trackindex>
						<clipindex>1</clipindex>
					</link>
				</clipitem>
				<enabled>TRUE</enabled>
			</track>
		</audio>
	</media>
</clip>
</xmeml>

# 6  
Old 04-09-2010
A general sed statement to get all languages changed:

Code:
sed 's@\(<pathurl>file:///Volumes/\)White%20Stage/2%20Stage%20capture/\(\(DE\|FR\|ES\)_.*\.mov</pathurl>\)$@\1ctu/\3/4.\\ Final\\ Cut\\ Pro\\ Documents/Capture\\ Scratch/\2@' <your_xml_file>

This will work for all three languages you've mentioned.

However, what you must do is redirect this to a different file. It must be a different file.
You can then rename the new file to the old file name.

So a bare bones shell script might look like the following:

Code:
#!/bin/sh

oldFile=$1
newfile=${oldFile}.new    #just put a ".new" extension onto the oldFile

sed 's@\(<pathurl>file:///Volumes/\)White%20Stage/2%20Stage%20capture/\(\(DE\|FR\|ES\)_.*\.mov</pathurl>\)$@\1ctu/\3/4.\\ Final\\ Cut\\ Pro\\ Documents/Capture\\ Scratch/\2@' ${oldFile} > ${newFile}

mv ${newFile} ${oldFile}

Obviously test this before moving the newFile to oldFile to make sure you are getting the results you want.
# 7  
Old 04-12-2010
Thanks for this.

I've tested it but currently it's not replacing the data in the XML file - it writes a new file but that's it. Old file path present. :s

Code:
#!/bin/sh

oldFile=$1
newFile=${oldFile}.new    #just put a ".new" extension onto the oldFile

clear
echo --XML REWRITE--
echo oldfile is ${oldFile}
echo newfile is ${newFile}


sed 's@\(<pathurl>file:///Volumes/\)White%20Stage/1%20white%20capture/\(\(DE\|FR\|ES|GE\)_.*\.mov</pathurl>\)$@\1ctu/\3/4.\\ Final\\ Cut\\ Pro\\ Documents/Capture\\ Scratch/\2@' ${oldFile} > ${newFile}

#mv ${newFile} ${oldFile}

If I try this at the command line:

Code:
sed 's@\(<pathurl>file:///Volumes/\)White%20Stage/1%20white%20capture/\(\(DE\|FR\|ES|GE\)_.*\.mov</pathurl>\)$@\1ctu/\3/4.\\ Final\\ Cut\\ Pro\\ Documents/Capture\\ Scratch/\2@' DE_APPS_SLATE001_TK001-10h17m05s13-white.XML

It outputs this:

Code:
</xmeml></media></audio></track><enabled>TRUE</enabled>dex>1</clipindex>x>e></masterclipid>white%20capture/DE_APPS_SLATE001.mov</pathurl>


..which just further confuses me.


(I'm using OS X 10.6 Server by the way...)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert subnode in xml file using xmlstarlet or any other bash command?

I have multiple xml files where i want to update a subnode if the subnode project points to different project or insert a subnode if it doesn't exist using a xmlstarlet or any other command that can be used in a bash script. I have been able to update the subnode project if it doesn't point to... (1 Reply)
Discussion started by: Sekhar419
1 Replies

2. Shell Programming and Scripting

Is it possible to change paths inside a bash script?

i have some script with some paths inside it. The idea is to some files which is on desktop copy and move to another location. Problem is that inside script is similar to this: cp test1.zip /root/help/ because I allways have another zip files, does it possible to have some input which ask me... (18 Replies)
Discussion started by: tomislav91
18 Replies

3. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

4. Shell Programming and Scripting

Comparing delta values of one xml file in other xml file

Hi All, I have two xml files. One is having below input <NameValuePair> <name>Daemon</name> <value>tcp:7474</value> </NameValuePair> <NameValuePair> <name>Network</name> <value></value> </NameValuePair> ... (2 Replies)
Discussion started by: sharsour
2 Replies

5. Shell Programming and Scripting

Help required in Splitting a xml file into multiple and appending it in another .xml file

HI All, I have to split a xml file into multiple xml files and append it in another .xml file. for example below is a sample xml and using shell script i have to split it into three xml files and append all the three xmls in a .xml file. Can some one help plz. eg: <?xml version="1.0"?>... (4 Replies)
Discussion started by: ganesan kulasek
4 Replies

6. Shell Programming and Scripting

reading file paths from a text file

Hello all, I am new to scripting and here is what I want to get done. I need to run some processing on many files. I have a whole bunch of files among which there are a few that I am interested in. What I need to do is to write a script which reads the file paths from a text file. line by... (1 Reply)
Discussion started by: pavo-cristatus
1 Replies

7. Shell Programming and Scripting

KSH - help needed for creating a script to generate xml file from text file

Dear Members, I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input. The contents of a single cell of INFO column is like: Area:app - aam Clean Up Criteria:... (0 Replies)
Discussion started by: Yoodit
0 Replies

8. Shell Programming and Scripting

bash extract all occurences delimited from <name> and </name> tags from an xml file

I need to extract all text delimited from <name> and </name> tags from an xml file, but not only first occurence. I need to extract all occurences. I've tried with this command: awk -F"<name>|</name>" 'NF>2{print $2}' but it give only first occurence. How can i modify it? (18 Replies)
Discussion started by: ingalex
18 Replies

9. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies
Login or Register to Ask a Question