If I understand correctly, you want to modify each XML file in-place using data extrapolated from another file. For example, the source data might look like:
one|fluffy|slurm|unicorns|animal.xml
two|yellow|flarn|moons|mineral.xml
three|blue|jalaroot|stars|mineral.xml
The the XML ... well, I don't need to provide an example. I gather you want to replace the <element>
in each XML file with $2 and $4 concatenated. If this is incorrect, please clarify it in your question.
So here's an option.
#!/bin/sh
awk -F'|' '{print $5,$2$4}' source.txt | while read file data; do
case "$data" in
*#*) echo "ERROR: invalid data ('$data')" >&2 ;;
*) if [ -f "$file" ]; then
sed -ri -e "s#<element>[^>]+</element>#<element>$data</element>#" "$file"
else
echo "ERROR: no such file: '$file'" >&2
fi
;;
esac
done
The idea here is that we'll take the data as a set of shell variables, $file
and $data
, then step through each substitution in a while loop. The substitution is done using sed
"in-place" (-i). Read the man page for your sed
implementation and back up your data before attempting to use this.
Note that this is actually POSIX-compatible, and doesn't require bash. (Though it will work fine in bash as well.)
PROVISOS:
- In its current state, this fails if filenames contain whitespace.
- If data must include other XML tags (i.e. ">" characters) then the regex in sed should be improved. (Notwithstanding the fact that you can't parse HTML with regex.)