Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 09-08-2005
Registered User
 

Join Date: Sep 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
CSV to XML

Iam pretty new to UNIX and would like to convert a CSV to an XML file using AWK scripts. Can anybody suggest a solution? My CSV file looks something like this :

Serial No Growth% Annual % Commission % Unemployed %
1 35% 29% 59% 42%
2 61% 37% 34% 86%
3 68% 47% 62% 18%
4 78% 83% 91% 78%
5 53% 44% 67% 56%
Sponsored Links
  #2  
Old 09-09-2005
photon's Avatar
Registered User
 

Join Date: Jul 2002
Posts: 164
Thanks: 0
Thanked 0 Times in 0 Posts
You mean like this?

"35%","29%","59%","42%"
"61%","37%","34%","86%"
"68%","47%","62%","18%"
"78%","83%","91%","78%"
"53%","44%","67%","56%"

Otherwise it would not be CSV file.
  #3  
Old 09-09-2005
Registered User
 

Join Date: Sep 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
CSv to XML

Yes, that's what I mean.
  #4  
Old 09-10-2005
Registered User
 

Join Date: Sep 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
AWK Script!

Hi photon,
Can you help me with that problem I had - converting my CSV file to XML using AWK Scripts? I need it really urgently and I need your help. Since Iam new to Unix, it is making it harder to deduce and try to understand quickly. Please help.

Thanks.
  #5  
Old 09-10-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,611
Thanks: 2
Thanked 36 Times in 33 Posts
Quote:
Originally Posted by pjanakir
Hi photon,
Can you help me with that problem I had - converting my CSV file to XML using AWK Scripts? I need it really urgently and I need your help. Since Iam new to Unix, it is making it harder to deduce and try to understand quickly. Please help.

Thanks.
given data file - pj.txt

Code:
SerialNo Growth% Annual% Commissio% Unemployed%
1 35% 29% 59% 42%
2 61% 37% 34% 86%
3 68% 47% 62% 18%
4 78% 83% 91% 78%
5 53% 44% 67% 56%

and script - pj.awk

Code:
BEGIN {OFS = "\n"}
NR == 1 {for (i = 1; i <=NF; i++)
            tag[i]=$i
         print "<" node "XML>"}
NR != 1 {print "   <" node ">"
         for (i = 1; i <= NF; i++)
            print "      <" tag[i] ">" $i "</" tag[i] ">"
         print "   </" node ">"}
END {print "</" node "XML>"}

nawk -v node=whatever -f pj.awk pj.txt

produces:

Code:
<whateverXML>
   <whatever>
      <SerialNo>1</SerialNo>
      <Growth%>35%</Growth%>
      <Annual%>29%</Annual%>
      <Commissio%>59%</Commissio%>
      <Unemployed%>42%</Unemployed%>
   </whatever>
   <whatever>
      <SerialNo>2</SerialNo>
      <Growth%>61%</Growth%>
      <Annual%>37%</Annual%>
      <Commissio%>34%</Commissio%>
      <Unemployed%>86%</Unemployed%>
   </whatever>
   <whatever>
      <SerialNo>3</SerialNo>
      <Growth%>68%</Growth%>
      <Annual%>47%</Annual%>
      <Commissio%>62%</Commissio%>
      <Unemployed%>18%</Unemployed%>
   </whatever>
   <whatever>
      <SerialNo>4</SerialNo>
      <Growth%>78%</Growth%>
      <Annual%>83%</Annual%>
      <Commissio%>91%</Commissio%>
      <Unemployed%>78%</Unemployed%>
   </whatever>
   <whatever>
      <SerialNo>5</SerialNo>
      <Growth%>53%</Growth%>
      <Annual%>44%</Annual%>
      <Commissio%>67%</Commissio%>
      <Unemployed%>56%</Unemployed%>
   </whatever>
</whateverXML>

  #6  
Old 09-10-2005
Registered User
 

Join Date: Sep 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
AWK Script!

vgersh99,
thanks for your help. Can you explain it step by step, so I can understand better. When I open my unix box, and say openssh servername
and enter user name and password. after that how do I execute this script?
Do let me know as Iam entirely new to this project. I would be very greatly indebted to you for your timely help.

Thanks,
pjanakir
  #7  
Old 09-10-2005
Registered User
 

Join Date: Sep 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
CSV File!

Hi vgersh99,
Also the file that Iam sending should only be a CSV file, and not a text file. You said pj.txt, it should be pj.csv. Just to let you know about it.

Thanks,
pjanakir
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




All times are GMT -4. The time now is 07:50 AM.