regex - BASH script to rename XML file to an attribute value -
i have a lot of .xml
files structured same way:
<parent id="idvalue" attr1="val1" attr2="val2" ...> <child attr3="val3" attr4="val4" ... /> <child attr3="val5" attr4="val6" ... /> ... </parent>
each file has 1 <parent>
element 1 id
attribute.
all of files (almost 1,700,000 of them) named part.xxxxx xxxxx random number.
i want name each of files idvalue.xml
, according sole id
attribute file's content.
i believe doing bash script fastest , automated way. if there other suggestions, love hear them.
my main problem not able (don't know how) idvalue
in specific file, use mv file.xxxxx idvalue.xml
command.
first iterate through xml files using find
:
find -maxdepth 1 -name 'part*.xml' -exec ./rename_xml.sh {} \;
the line above execute rename_xml.sh
every xml file, passing file name command argument script.
rename_xml.sh
should this:
#!/bin/bash // id using xpath. might need // install xmllint if not present. // xpath query return string (try it!): // // id="idvalue" // // using sed extract value id=$(xmllint --xpath '//parent/@id' "$1" | sed -r 's/[^"]+"([^"]+).*/\1/') mv -v "$1" "$id.xml"
don't forget to
chmod +x rename_xml.sh
Comments
Post a Comment