1

I am trying to write a shell script to create multiple .xml files based on the data read from .csv. I need the help on how to make the loop for each .xml file creation.

Rough algorithm for this script

  • set the in and out files in the script
  • create any function or do while or for to read the each line from .csv
  • create a .xml file with the name of emp-gid in any separate folder

Sample Employee.csv

23456,USD
89012,EUR
12345,CHF
34567,SGD

Sample .xml structure

<?xml version="1.0" encoding="UTF-8"?>
<fi:Organisation>
        <fi:Company>
                <fi:DEPARTMENT>Account</fi:DEPARTMENT>
                <fi:department-key>ABC123</fi:department-key>
                <fi:department-id>121</fi:department-id>
        </fi:Company>
        <fi:Employee>
                <fi:emp-gid>DE0008475021</fi:emp-gid>
                <fi:country>EUR</fi:country>
        </fi:body>
</fi:organisation>    

My shell script

#!/bin/bash
InputFile="Employee.csv"
OutputFile="Emp_gid.xml"
echo '<?xml version="1.0"?>' > $OutputFile
echo '<fi:organisation>' >> $OutputFile
echo     '<fi:Company>' >> $OutputFile
echo         '<fi:DEPARTMENT>Account</fi:DEPARTMENT>' >> $OutputFile
echo         '<fi:department-key>ABC123</fi:department-key>' >> $OutputFile <-- want to increment by one for all xml file (ABC123,ABC124..) -->
echo         '<fi:department-id>121</fi:department-id>' >> $OutputFile <-- want to increment by one for all xml file (121,122,123,..) -->
echo     '</fi:Company>' >> $OutputFile
while IFS=$',' read -r -a arry
do
  echo       '  <fi:Employee>' >> $OutputFile
  echo          '    <fi:emp-gid>'${arry[0]}'</fi:emp-gid>' >> $OutputFile
  echo          '    <fi:country>'${arry[1]}'</fi:country>' >> $OutputFile
  echo      '  </fi:Employee>' >> $OutputFile
done < $InputFile
echo '</fi:organisation>' >> $OutputFile

This above script only generates one .xml file with all records. But I would like to expect some function or script which can generate separate .xml files for each row in the .csv file and put it into the separate folder.

1

I would do this:

# a printf format string
xml_template=$(cat <<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<fi:Organisation>
        <fi:Company>
                <fi:DEPARTMENT>Account</fi:DEPARTMENT>
                <fi:department-key>%s%d</fi:department-key>
                <fi:department-id>%d</fi:department-id>
        </fi:Company>
        <fi:Employee>
                <fi:emp-gid>%s</fi:emp-gid>
                <fi:country>%s</fi:country>
        </fi:body>
</fi:organisation>    
XML
)

dept_key_prefix=ABC
dept_key=100         # choose suitable initial values
dept_id=100

while IFS=, read -r emp_gid country; do
    printf "$xml_template" \
            $dept_key_prefix \
            $((dept_key++)) \
            $((dept_id++)) \
            "$emp_gid" \
            "$country" \
        > "$emp_gid.xml"
done < Employee.csv
  • Thanks Glenn..Your script works. But when i tried to redirect to the generated xml to some other directory , the behavior is different and i could only see one xml is generated. Lets say i want to redirect the generated xml to this path "Xml_dir=/tmp/pos/xmldir". I declared this variable and used as follows ---$Xml_dir/"emp_gid.xml"-- in your script. But it doesn't work as expected. can you please correct what i missed. – karthik Dec 24 '18 at 4:12
  • you appear to have removed the $ from the $emp_gid variable. – glenn jackman Dec 24 '18 at 4:34
  • After added $ as suggested , it works fine. In addition, i am also planning to send these generated xml files to input of one of our another existing script. Like ourscript.sh <gid01.xml> and upon successful execution,this script continue to pick another xml. Would you suggest any logic/sample code to achieve this in your the above script? Thanks.. – karthik Dec 24 '18 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.