Você pode escrever um modelo printf
:
info_template=" <Info>\n\
<id>%s</id>\n\
<date>%s</date>\n\
<time>%s</time>\n\
<num>%s</num>\n\
<type>%s</type>\n </Info>\n"
Em seguida, construa o arquivo XML:
#!/bin/bash
if [ "$(egrep -l 'NU' /home/archive/* | xargs egrep -l 'SE')" ]
then
egrep -l 'NU' /home/archive/* | xargs egrep -l 'SE' > /tmp/result.txt
chmod 777 /tmp/result.txt
# the output XML file
my_xml_file=~/data.xml
# the spaces are for pretty-ing up the output
info_template=" <Info>\n\
<id>%s</id>\n\
<date>%s</date>\n\
<time>%s</time>\n\
<num>%s</num>\n\
<type>%s</type>\n </Info>\n"
# add the opening tags
echo '<?xml version="1.0"?>' >> $my_xml_file
echo '<List>' >> $my_xml_file
a=1
while read LINE
do
path[$a]=$LINE
file[$a]='basename $LINE'
cat $LINE | awk '{gsub("^",RS);print}' > /tmp/tmp2.result.txt
chmod 777 /tmp2/tmp.result.txt
cat /tmp/tmp.result.txt | awk '{gsub("-",RS);print}' > /tmp/tmp.result.txt
chmod 777 /tmp/tmp.result.txt
ID[$a]='sed -n '7p' < /tmp/tmp.result.txt'
DATE[$a]='sed -n '10p' < /tmp/tmp.result.txt'
TIME[$a]='sed -n '11p' < /tmp/tmp.result.txt'
NUM[$a]='sed -n '14p' < /tmp/tmp.result.txt'
TYPE[$a]='sed -n '26p' < /tmp/tmp.result.txt'
# echo -e "File: ${file[$a]}" >> /tmp/result.result.tmp
# echo -e "Type:${TYPE[$a]}\ID: ${ID[$a]}\tDATE:${DATE[$a]}\tTIME:${TIME[a]}\tNUM: ${NUM[$a]}" >> /tmp/result.result.tmp
printf "$info_template" "$ID[$a]" \
"$DATE[$a]" \
"$TIME[$a]" \
"$NUM[$a]" \
"$TYPE[$a]" >> $my_xml_file
done < /tmp/result.txt
# don't forget the closing tag
echo '</List>' >> $my_xml_file
fi