Remove trailing space in Gawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove trailing space in Gawk
# 1  
Old 03-14-2015
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 :-


Code:
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

Code:
Name	Identity	Identity No
XYZ	PIN	5678
LKY	PIN	234


Below is the output :-

Code:
    <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 :-

Code:
 <Name="Name">
              		<es:Identity>Identity No</es:Identity> 
    <Name="XYZ">
              		<es:PIN>5678</es:PIN> 
    <Name="LKY">
              		<es:PIN>234</es:PIN>


Last edited by Don Cragun; 03-14-2015 at 05:58 PM.. Reason: Add CODE tags.
# 2  
Old 03-14-2015
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:
Code:
gawk -F ',' '

	BEGIN {}

to:
Code:
gawk -F '\t' '

and see if it works any better.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-14-2015
my mistake, i have copied pasted from excel so it was looking like that,
input is

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

I tried, but again i am getting space
Code:
<es:PIN>234
</es:PIN>

instead of
Code:
<es:PIN>234</es:PIN>

# 4  
Old 03-14-2015
Please show us the output from the command:
Code:
od -bc input_file

where input_file is the name of your CSV input file.

What operating system and shell are you using?
# 5  
Old 03-15-2015
Code:
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
# 6  
Old 03-15-2015
The file.is in Windows format with trailng carriage return characters

Convert to Unix first:
Code:
tr -d '\r' < oldfile > newfile

# 7  
Old 03-15-2015
You seem to consistently mix up file names ...Database.xml and ...Databse.xml
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk --- produce the output in pattern space instead of END space

hi, I'm trying to calculate IP addresses and their respective calls to our apache Server. The standard format of the input is HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST... (2 Replies)
Discussion started by: busyboy
2 Replies

2. Shell Programming and Scripting

Remove trailing space from file and folder names

I have a folder that contains many sub folders and files. This tree has to be backed up to an archive system. According to the tech support, one of the archives is failing to back up due to the possibility of trailing spaces on file and folder names. Therefore, I would like to have a script... (16 Replies)
Discussion started by: vipertech
16 Replies

3. Shell Programming and Scripting

Remove trailing number

I have some strings such as ABC1 ABC2 TYFASDD12 They will only have letters and numbers. In each case I want to remove the last digit? The lengths will vary. So a hard coded substr won't work. What do I do? if it doesn't end in a number, I don't want to remove any characters. (6 Replies)
Discussion started by: guessingo
6 Replies

4. Shell Programming and Scripting

Replacing trailing space with single quote

Platform : RHEL 5.8 I want to end each line of this file with a single quote. $ cat hello.txt blueskies minnie mickey gravity snoopyAt VI editor's command mode, I have used the following command to replace the last character with a single quote. ~ ~ ~ :%s/$/'/gNow, the lines in the... (10 Replies)
Discussion started by: John K
10 Replies

5. Shell Programming and Scripting

Remove trailing space

Hi I am trying to remove trailing space from a string. value=${value%% } It is not working. What might be the issue with the above snippet. (7 Replies)
Discussion started by: munna_dude
7 Replies

6. Shell Programming and Scripting

Remove trailing 0 from the field

Hi Freinds, I have file1.txt as below file1.txt 1521894~~-0.400~201207 1521794~~-0.486~201207 152494~~-0.490~201207 152154894~~-0.490~201207 1521894354~~-0.489~201207 expected output : 1521894~~-0.4~201207 1521794~~-0.486~201207 152494~~-0.49~201207... (9 Replies)
Discussion started by: i150371485
9 Replies

7. Shell Programming and Scripting

Delete trailing white space

I have a string "disk0 with a trailing white space after it" but I want to get rid of this white space from right to left so that I am left with "disk0" only. Using sed 's/ $//g' doesn't seem to work Any ideas ? Thanks (5 Replies)
Discussion started by: cillmor
5 Replies

8. Shell Programming and Scripting

Remove trailing G

Hello, I am trying to write a script that will calculate the amount of data remaining in a storage volume. I'm running Tru64 Unix version 5.1B patch kit 6. The script is being run against an AdvFS domain. I am programming in Korn Shell version M-11/16/88f. The basic idea is that I want to run df... (3 Replies)
Discussion started by: Heathe_Kyle
3 Replies

9. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies

10. Shell Programming and Scripting

re: removing trailing space from lines

Not sure why this thread was closed without any explanation, but you can do what you're asking with sed 's/]*$//g' < sourceFile > destFile (1 Reply)
Discussion started by: oombera
1 Replies
Login or Register to Ask a Question