Remove trailing space in Gawk

Hi,
I have simply made a shell script to convert *.csv to *.xml file. Xml file is required for input 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
</es: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>

I find it hard to believe that you got the output you said you got from that script and sample input file. The field separator in your sample input file seems to be a tab character, not a comma; but your awk script is looking for a comma ( -F ',' ).

Try changing the first four lines of your awk script from:

gawk -F ',' '

	BEGIN {} 

to:

gawk -F '\t' '

and see if it works any better.

1 Like

my mistake, i have copied pasted from excel so it was looking like that,
input is

Name,Identity,Identity No
XYZ,PIN,5678
LKY,pin,234

I tried, but again i am getting space

<es:PIN>234
</es:PIN>

instead of

<es:PIN>234</es:PIN>

Please show us the output from the command:

od -bc input_file

where input_file is the name of your CSV input file.

What operating system and shell are you using?

od -bc sample.csv
0000000 116 141 155 145 054 111 144 145 156 164 151 164 171 054 111 144
          N   a   m   e   ,   I   d   e   n   t   i   t   y   ,   I   d
0000020 145 156 164 151 164 171 040 116 157 015 012 130 131 132 054 120
          e   n   t   i   t   y       N   o  \r  \n   X   Y   Z   ,   P
0000040 111 116 054 065 066 067 070 015 012 114 113 131 054 120 111 116
          I   N   ,   5   6   7   8  \r  \n   L   K   Y   ,   P   I   N
0000060 054 062 063 064 015 012
          ,   2   3   4  \r  \n
0000066

Ubuntu OS & sh

The file.is in Windows format with trailng carriage return characters

Convert to Unix first:

tr -d '\r' < oldfile > newfile

You seem to consistently mix up file names ...Database.xml and ...Databse.xml