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):

<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:

<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 :slight_smile:

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. :o

Does the path mentioned in output is always the same?

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

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.

/Volumes/ctu/DE
/Volumes/ctu/ES
/Volumes/ctu/FR

Try this

$ 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'

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. :slight_smile:

<?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>

A general sed statement to get all languages changed:

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:

#!/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.

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

#!/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:

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:

</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...)