Sample Header Ad - 728x90

Renaming xml file based on tag

0 votes
2 answers
168 views
I have a number of xml files. I need to rename each based on the specific tag (say tag1="alpha") inside the xml. I have figured out how to rename the file based on _single_ occurance of the specific tag.
find . -maxdepth 1 -name '*.xml' -exec /rename.sh {} \;
#!/bin/bash

tag1=$(sed 's/.*tag1="\([^"]*\).*/\1/; q' "$1")
mv -v "$1" "$tag1.xml"
The problem: some files have multiple tag1 tags, e.g. tag1="alpha" tag1="beta" tag1="omega". With these files I need to also create a copies of the same file, but with the other names, beta.xml, omega.xml. I tried to cat with positional parameters but that didn't work (I am a beginner). **file structure example:**
cudg.el | ˈ;ke;je;lˈ;ke;dʒe;l | noun a short, thick stick used as a weapon. verb (, ; British ) [with object] beat with a cudgel : they would lie in wait and cudgel her to death . PHRASES (also ) British think hard about a problem : she cudgeled her brains, trying to decide what had caused such an about-face . start to defend or support someone or something strongly : there was no one else to take up the cudgels on their behalf . ORIGIN Old English cycgel, of unknown origin.
Asked by Simon Ante (1 rep)
Jul 20, 2022, 07:23 PM
Last activity: Mar 10, 2024, 05:07 PM