I have a multi-line pipe delimited file that I want to use to write an XML document. I was hoping to use a shell script to do this. Does anyone have any examples or advice they are willing to share.
Here is what the pipe delimited file would look like:
Name1|PHX|AZ|
.
.
.
Name5|MSP|MN
Here is what I would want the xml to look like:
<parent-element>
<place name="Name1">
<location city="PHX" state="AZ"/>
</place>
.
.
.
<place name="Name5">
<location city="MSP" state="MN"/>
</place>
</parent-element>
My plan was to read the file in and try to parse the file line by line. I thought that I could use "while read line" and then use the cut command to build the xml. But this approach appears to require that I specify a file for the cut command and the it runs on the whole file and not the link of the file.
FILE="$1"
# make sure file exists and is readable
if [ "$1" == "" ]; then
echo "Specify input file"
exit 1
elif [ ! -f $FILE ]; then
echo "$FILE : does not exist"
exit 2
elif [ ! -r $FILE ]; then
echo "Cannot read: $FILE"
exit 3
fi
echo "<?xml version= \"1.0\" encoding= \"utf-8\"?>"
echo "<parent-element>"
exec 0<$FILE
while read line
do
# cut -f4 -d\|
# if this works, just use it below as applicable
echo " <place name=\"\">"
echo " <location city=\"\" state=\"\">"
echo " </place>"
done
echo "</parent-element>
Here is what the pipe delimited file would look like:
Name1|PHX|AZ|
.
.
.
Name5|MSP|MN
Here is what I would want the xml to look like:
<parent-element>
<place name="Name1">
<location city="PHX" state="AZ"/>
</place>
.
.
.
<place name="Name5">
<location city="MSP" state="MN"/>
</place>
</parent-element>
My plan was to read the file in and try to parse the file line by line. I thought that I could use "while read line" and then use the cut command to build the xml. But this approach appears to require that I specify a file for the cut command and the it runs on the whole file and not the link of the file.
FILE="$1"
# make sure file exists and is readable
if [ "$1" == "" ]; then
echo "Specify input file"
exit 1
elif [ ! -f $FILE ]; then
echo "$FILE : does not exist"
exit 2
elif [ ! -r $FILE ]; then
echo "Cannot read: $FILE"
exit 3
fi
echo "<?xml version= \"1.0\" encoding= \"utf-8\"?>"
echo "<parent-element>"
exec 0<$FILE
while read line
do
# cut -f4 -d\|
# if this works, just use it below as applicable
echo " <place name=\"\">"
echo " <location city=\"\" state=\"\">"
echo " </place>"
done
echo "</parent-element>