Help with find and replace in XML

Hello Guys,

I have this requirement with several hundred files.

I have this first set of xml's files with the following tags spread across the file

FILE in SET A

<Name>Lion</Name>
<Age>15</Age>
.....
....
...

<Date>2009-12-12</Date>

Now i have this another set of files which also have the same tags again randomly spread across in the file.

FILE SET B

<Name>xxxx</Name>
......
......

<Age>yyy</Age>
.....
....

<Date>zzzz</Date>

My requirement is that i have around 6 matching tags in both the files.I need replace the data between those matching the tags in SET B with that of SET A.

I am currently thinking in 2 lines:

  1. Either replacing the tags completely.
  2. Replacing only data within the matching tags.

Can anyone please suggest me how to implement this?

Cheers!!

bash. 4.0 with associative arrays

# bash 4.0
declare -A dict
while read -r line
do
    case "$line" in
        "<Name>"*|"<Age>"*)
            tag=${line%%>*}
            tag=${tag#*<}
            val=${line#*>}
            val=${val%%<*}
            dict["$tag"]="$val"
            ;;
    esac
done <"file"
while read -r line
do
    case "$line" in
       "<Name>"*|"<Age>"*)
        tag=${line%%>*}
        tag=${tag#*<}
        line="<$tag>${dict["$tag"]}</$tag>"
    esac
    echo $line
done <"file2"


you could do the same with awk, otherwise, ideally you should get a good XML parser.

Will this work on korn shell, can you suggest the awk option please?

Also in what you have shown below, i have as mentioned before have 6 tags which need data replacement, so how do i go about?? :confused:

Is there a one to one correspondence between the files in setA and the files in setB, i.e. does the first file in setA correspond to the first file in setB and so on?

No, there is no such correspondence.

So how are you supported to find the file in setB that is to be modified? Are saying that the "6 matching tags" will uniquely identify the particular file in setB which is to be modified?

How many files are in setA and how many files are in setB?