1

I'm trying to use hek2mgl's solution to a previous question and am getting an error message I hope you can help me with.

What I am trying to do: rename XML files so they reflect the contents of the identifier element.

This is the exact code I am using:

find . -maxdepth 1 -name 'part*.xml' -exec ./rename_xml.sh {} \; 

While my rename_xml script looks like this:

#!/bin/bash id=$(xmllint --xpath '//identifier' "$1" | sed -r 's/[^"]+"([^"]+).*/\1/') mv -v "$1" "$id.xml" 

A sample source XML file looks like this:

<root> <title>title</title> <identifier>001</identifier> </root> 

I get the following error message when I run the find command.

sed: illegal option -- r usage: sed script [-Ealn] [-i extension] [file ...] sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...] XPath set is empty ./.xml -> .xml 

My guess it that there are two things happening here - a non-working version of sed (illegal -r option), and a problem with my XPath. How can I change this script to work as intended?

1
  • I want to do exactly what it says in the title of the question: Rename XML files based on contents of an element. Commented Mar 26, 2019 at 13:09

1 Answer 1

2

Would something like this work for ya?

#!/bin/bash find . -maxdepth 1 -name "part*.xml" | while read file do id=$(xmllint --xpath 'string(//identifier)' $file) echo "mv $file $id.xml" mv $file $id.xml done 
Sign up to request clarification or add additional context in comments.

2 Comments

Adam vonNieda, this works perfectly, and I think I understand how it works except for the presence of $file at the end of line 5. Thank you!
You're welcome, that's just telling xmllint what file it's working on. Please mark as correct if you don't mind ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.