Register
It is currently Sat Oct 25, 2014 2:45 am

Loop through CSV to create XML


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Mar 02, 2011 9:18 pm   

Joined: Wed Mar 02, 2011 9:15 pm
Posts: 2
Hello all, first and foremost I want to apologize if this has been answered somewhere, but I am looking to do the following:

I have 500 records I need put in XML format. The records are currently in CSV format. I can use curl to upload a record in XML format using our applications API. I was under the impression that I could generate 1 large XML file and upload the entire group.

I have a script that will read in data from a CSV and output it to a file in XML format. I got the script the way I thought I needed it working, which was to create a new xml file, append a line to it, then loop through the csv and echo my string repeatedly inputing the variables from the CSV file. I could then upload the XML file where I needed it.

As it turns out, it seems the API will only allow me to upload 1 record at a time. (ALMOST pointless IMO.) So what I am looking to do is change the way the script loops through the CSV file, and instead of creating 1 large file, I would like the script to loop through the CSV one line at a time, create the XML file of only 1 record, upload it using curl, delete itself (or its contents) and repeat that process throughout the entire CSV file.

I am just getting familiar with loops, so Im not sure how I would even start this process, or even word what I want properly to search google. (Which I have been for the last two days, thats how I frankenstiened what I have below. Any help would be greatly appreciated. (Also, just because it bothers me, can anyone help get the line breaks out everytime it echoes a new line?)

The script is as follows:
Code:
#!/bin/bash

echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' >> xmldata.xml

#:Read variables from XMLInput.csv:#
while read inputline;do
line0="$(echo $inputline | cut -d, -f1)"
line1="$(echo $inputline | cut -d, -f2)"

#:Set auto increment ID:#
idAdd0=$((idAdd0 +1))

#:removes trailing alpha characters leaving only the IP address:#
cutLine0=${line0//[^0-9.]/}

#:Strips trailing octet and appends .1:#
startIP=`echo $cutLine0 | sed 's/\.[0-9]*$/.1/'`

#:Strips trailing octet and appens 254:#
endIP=`echo $cutLine0 | sed 's/\.[0-9]*$/.254/'`

#:echos line to txt file as XML:#
echo "<network_segment><id>"$idAdd0"</id><name>$line0</name><starting_address>$startIP</starting_address><ending_address>$endIP</ending_address><distribution_point/><url/><netboot_server/><swu_server/><building>$line1</building><department/><override_buildings>false</override_buildings><override_departments>false</override_departments></network_segment>" >> xmldata.xml

done < XMLInput.csv

exit 0


Top
 Profile  
 PostPosted: Thu Mar 03, 2011 12:39 am   

Joined: Wed Mar 02, 2011 9:15 pm
Posts: 2
What I ended up doing was having the script create 500 separate XML files in a folder by adding my auto increment variable to the XML file name,
then curling each file as a separate entry. A litte dirtier than I wanted, but in the end, I didn't have to manually enter 500 subnets
and define there locations in a website. Job Done.

Script if interested:

Code:
#!/bin/bash

#:Read variables from XMLInput.csv:#
while read inputline;do
line0="$(echo $inputline | cut -d, -f1)"
line1="$(echo $inputline | cut -d, -f2)"
echo

#:Set auto increment ID:#
idAdd0=$((idAdd0 +1))

#:removes trailing alpha characters leaving only the IP address:#
cutLine0=${line0//[^0-9.]/}

#:Strips trailing octet and appends .1:#
startIP=`echo $cutLine0 | sed 's/\.[0-9]*$/.1/'`

#:Strips trailing octet and appens 254:#
endIP=`echo $cutLine0 | sed 's/\.[0-9]*$/.254/'`

#:echos line to txt file as XML:#
echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'"<network_segment><id>"$idAdd0"</id><name>$line0</name><starting_address>$startIP</starting_address><ending_address>$endIP</ending_address><distribution_point/><url/><netboot_server/><swu_server/><building>$line1</building><department/><override_buildings>false</override_buildings><override_departments>false</override_departments></network_segment>" >> xmldata$idAdd0.xml

#:upload XML file to JSS:#
`curl -k -v -u account:Password https://server address/JSSResource/networksegments/name/SegmentName -T "xmldata$idAdd0.xml" -X POST`

done < XMLInput.csv

exit 0


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP