Wednesday, May 18, 2011

Solaris shell script for parsing large XML file into multiple XML files. Create PDF and prepare meta data for email sending mechanism

This script I wrote because billing system in my company produces complex and big XML output - 1 file with multiple invoices inside. Request from business department was to split this large file into multiple XML files - one file per invoice. In same time, header and footer section from large XML must be merged into every single small file. Main goal is to create PDF invoice file and send it as attachment by email using customer's email address.

After splitting process, every file should be used as input for generating PDF file. For generating PDF file from XML I am using DETEC's product "LaserSoft". At top of all, meta data file should be created with email client address extracted from XML file and name of PDF file generated for that invoice. This meta data file will be used later in post process of sending PDF file to customer's email address.




[quote]
#!/bin/sh
rm /u01/app/company/fileio/billing/efakturi/meta.txt
input_fajl=$1
red=0
sleden_red=0
broj_linii=`cat $input_fajl | /usr/xpg4/bin/grep -n '' | cut -d ":" -f 1`
pocetok_footer=`cat $input_fajl | /usr/xpg4/bin/grep -n '' | cut -d ":" -f 1`
cat $input_fajl | /usr/xpg4/bin/grep -n ''| cut -d ">" -f2 | cut -d "<" -f 1 > ./tmp/kunde
nawk -v pocetok_footer="$pocetok_footer" -v krajna_linija="$krajna_linija" 'NR== pocetok_footer,NR== krajna_linija' $input_fajl > ./tmp/footer
nawk -v kraj_header="$kraj_header" 'NR==1,NR== kraj_header' $input_fajl > ./tmp/ header
while [ $broj_linii -gt 0 ]
do
red=$red+1
sleden_red=$red+1
linija=`awk 'NR=='$red ./tmp/client_pocetoci`
sledna_linija=`awk 'NR=='$sleden_red ./tmp/client_pocetoci`
kunde=`awk 'NR=='$red ./tmp/kunde`
if [ $broj_linii -eq 1 ]
then
sledna_linija=`cat $input_fajl | /usr/xpg4/bin/grep -n '' | c ut -d ":" -f 1 | tail -1`
fi
taman=`expr $sledna_linija - 1`
cat ./tmp/header > ./xml/$kunde.xml
nawk -v linija="$linija" -v taman="$taman" 'NR== linija,NR== taman' $input_fajl | /usr/xpg4/bin/grep -v '' | cut -d ">" -f2 | cut -d "<" -f 1 | grep -n '@' | cut -d ":" -f 2`
perl -pi -e s/"$izbrisi"/""/g ./xml/$kunde.xml
/opt/detec/lsoft/bin/lsmain -Q LS_PDF_FILE <./xml/$kunde.xml >./tmp/1
echo $adresa",e"$kunde.pdf >> /u01/app/company/fileio/billing/efakturi/meta.txt
pomal=$broj_linii
broj_linii=`expr $broj_linii - 1`
done


[/quote]

No comments:

Post a Comment