csv to xml file through gawk
Hi,
I have simply made a shell script to convert *.csv to *.xml file. Xml file is required to one tool. But i am getting space after last field. How can i remove it. Shell script is as follows :- if [ $# -ne 1 ] then echo "" echo "Wrong syntax, Databse_update.sh <Input_File.csv>" echo "" exit 0 fi rm Database.xml 1>/dev/null 2>&1 echo "" echo "Creating XML files" gawk -F ',' ' BEGIN {} {#if (print_ok == 0) { print " <Name=\""$1"\">" > "Database.xml" #print_ok=1 #} print " <es:"$2">"$3"</es:"$2"> " > "Database.xml" }' $1 if [ -f "Databse.xml" ] then echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > tmp1.txt echo " <fileFooter dateTime=\""`date`"\" />" >> tmp2.txt cat tmp1.txt Databse.xml tmp2.txt > tmp3.txt mv tmp3.txt "`date`"_Databse.xml rm tmp1.txt tmp2.txt 1>/dev/null 2>&1 rm Database.xml echo "" echo "File \""`date`"_Database.xml\" created " echo "" fi Input file (CSV) is like that Name Identity Identity No XYZ PIN 5678 LKY PIN 234 Below is the output :- <Name="Name"> <es:Identity>Identity No </es:Identity> <Name="XYZ"> <es:PIN>5678 </es:PIN> <Name="LKY"> <es:PIN>234 </PIN> I want output as below :- <Name="Name"> <es:Identity>Identity No</es:Identity> <Name="XYZ"> <es:PIN>5678</es:PIN> <Name="LKY"> <es:PIN>234</es:PIN> Thanks |
I don't understand the bash or anything about xml.
However, this may be useful. With this InFile ... Code:
<Name="Name"> Code:
awk '{outrec=$0 Code:
<Name="Name"> |
Firstly, please use [code][/code] tags around code / data to make it readable.
So your input file has zero commas in it so it is not technically a csv and this would also make -F',' pointless. If the data is in fact comma separated, then the current awk appears to work just fine: Code:
$ echo 'XYZ,PIN,5678' | awk -F, '{print " <Name=\""$1"\">";print " <es:"$2">"$3"</es:"$2"> "}' |
All times are GMT -5. The time now is 01:14 AM. |