LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2015, 03:32 PM   #1
stillrules
LQ Newbie
 
Registered: Mar 2015
Posts: 1

Rep: Reputation: Disabled
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
 
Old 03-15-2015, 11:28 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
I don't understand the bash or anything about xml.
However, this may be useful.

With this InFile ...
Code:
<Name="Name">
<es:Identity>Identity No
</es:Identity>
<Name="XYZ">
<es:PIN>5678
</es:PIN>
<Name="LKY">
<es:PIN>234
</PIN>
... this awk ...
Code:
awk '{outrec=$0
      if (substr($0,1,4)=="<es:") {getline; outrec=outrec $0}
      print outrec}' $InFile >$OutFile
... produced this OutFile ...
Code:
<Name="Name">
<es:Identity>Identity No</es:Identity>
<Name="XYZ">
<es:PIN>5678</es:PIN>
<Name="LKY">
<es:PIN>234</PIN>
Daniel B. Martin

Last edited by danielbmartin; 03-15-2015 at 11:50 AM. Reason: Simplify code, slightly.
 
Old 03-15-2015, 12:24 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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"> "}'
 <Name="XYZ">
 <es:PIN>5678</es:PIN>
Is this not the output you desire?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting 30 GB XML File to CSV file AkhilSJSU Linux - Enterprise 3 10-24-2014 04:17 AM
Help me convert this xml file to csv xmrkite Linux - Software 2 06-21-2014 09:48 PM
[SOLVED] awk or sed to use CSV as input and XML as template and output to a single file bridrod Linux - Newbie 6 03-13-2012 07:00 PM
looking for software to convert multiple csv files to a single xml file Rocket-boy Linux - Software 6 10-28-2009 10:03 AM
CSV null columns with gawk pcock Programming 1 03-30-2009 07:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration