xml - Outputting multiple files using XPath in bash -
i have directory of xml files. each file has own unique identifier. each file contains 1 or more references other files (in separate directory), have unique ids.
for example, have file named example01.xml
:
<file> <fileid>xyz123</fileid> <filecontents>blah blah blah</filecontents> <relatedfiles> <otherfile href='http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&id=1234'> <title>some resource</title> </otherfile> <otherfile href='http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&id=4321'> <title>some other resource</title> </otherfile> </relatedfiles> </file>
if file has multiple relatedfiles/otherfile
elements, need create copy of file each @href
, rename it, concatinating value of unique id in @href
value of fileid
. so, example, need create 2 copies of file example01.xml
, 1 named abc01_xyz123.xml
, named abc0002_xyz123.xml
. should scale create many copies there otherfile
elements.
right now, have bash script if there single otherfile
element, scripting skills limited , having trouble figuring out how process multiple otherfile
elements.
#!/bin/bash f in *.xml; name=`xpath -e 'string(//otherfile/@href)' $f 2> /dev/null` echo "moving" $f "to" ${name:3}.xml echo $name mv $f ${name:3}.xml done
thanks in advance.
something might work:
#!/bin/bash f in *.xml; fid=$(xpath -e '//fileid/text()' "$f" 2>/dev/null) uid in $(xpath -e '//otherfile/@href' "$f" 2>/dev/null | awk -f= '{gsub(/"/,"",$0); print $3}'); echo "moving $f ${fid}_${uid}.xml" cp "$f" "${fid}_${uid}.xml" done rm "$f" done
Comments
Post a Comment