Sponsored Content
Top Forums Shell Programming and Scripting Remove trailing space in Gawk Post 302938346 by stillrules on Saturday 14th of March 2015 04:02:32 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
FPRINTF(3)								 1								FPRINTF(3)

fprintf - Write a formatted string to a stream

SYNOPSIS
int fprintf (resource $handle, string $format, [mixed $args], [mixed $...]) DESCRIPTION
Write a string produced according to $format to the stream resource specified by $handle. PARAMETERS
o $handle -A file system pointer resource that is typically created using fopen(3). o $format - See sprintf(3) for a description of $format. o $args - o $... - RETURN VALUES
Returns the length of the string written. EXAMPLES
Example #1 fprintf(3): zero-padded integers <?php if (!($fp = fopen('date.txt', 'w'))) { return; } fprintf($fp, "%04d-%02d-%02d", $year, $month, $day); // will write the formatted ISO date to date.txt ?> Example #2 fprintf(3): formatting currency <?php if (!($fp = fopen('currency.txt', 'w'))) { return; } $money1 = 68.75; $money2 = 54.35; $money = $money1 + $money2; // echo $money will output "123.1"; $len = fprintf($fp, '%01.2f', $money); // will write "123.10" to currency.txt echo "wrote $len bytes to currency.txt"; // use the return value of fprintf to determine how many bytes we wrote ?> SEE ALSO
printf(3), sprintf(3), sscanf(3), fscanf(3), vsprintf(3), number_format(3). PHP Documentation Group FPRINTF(3)
All times are GMT -4. The time now is 05:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy