Help with converting XML to Flat file | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


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

Help with converting XML to Flat file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-19-2012
karumudi7 karumudi7 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 22 February 2015, 9:33 PM EST
Location: India
Posts: 111
Thanks: 19
Thanked 1 Time in 1 Post
Help with converting XML to Flat file

Hi Friends,

I want to convert a XML file to flat file.

Sample I/p:


Code:
<?xml version='1.0' encoding='UTF-8' ?>
<DataFile xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' contactCount='4999' date='2012-04-14' time='22:00:14' xsi:noNamespaceSchemaLocation='gen
.xsd'>
<Contact id="1">
  <Name>
    <FirstName>CHERLY</FirstName>
    <LastName>MORGA</LastName>
  </Name>
  <Address>
    <Street>401 </Street>
    <City>TRINIDAD</City>
    <State>CO</State>
    <Zip>81082</Zip>
  </Address>
  <ContactInfo>
    <Email>morga@msn.com</Email>
    <EvePhone>719-1</EvePhone>
  </ContactInfo>
  <Activity date="2012-04-04" source="Business Center" subSource="ES 2012 BC  22461" tdm_seq_num="EF_14832004" time="1
0:48:54">
    <Product>
      <POI code="15866" year="2012"/>
      <POI code="1-1ED0DJ" year="2012"/>
    </Product>
    <Survey>
      <Question1>
        <Response>4-6 months</Response>
      </Question1>
      <Question2>
        <Response>Moab, UT</Response>
      </Question2>
      <Question3>
        <Response>N</Response>
      </Question3>
      <Question4>
        <Response>No</Response>
      </Question4>
    </Survey>
  </Activity>
</Contact>
<Contact id="2">
  <Name>
    <FirstName>Gur</FirstName>
    <LastName>Singh</LastName>
  </Name>
  <Address>
    <Street>Garrison</Street>
    <City>Jersey City</City>
    <State>NJ</State>
    <Zip>07306</Zip>
  </Address>
  <ContactInfo>
    <Email>gursingh@gmail.com</Email>
    <EvePhone>2019</EvePhone>
  </ContactInfo>
  <Activity date="2012-04-05" source="Autoshow" subSource="ES 2012 Marketing 22402" tdm_seq_num="EF_14837438" time="
14:23:50">
    <Product/>
    <Survey>
      <Question1>
        <Response>Undecided</Response>
      </Question1>
      <Question2>
        <Response>New York, NY</Response>
      </Question2>
      <Question3>
        <Response>Y</Response>
      </Question3>
      <Question4>
        <Response>No</Response>
      </Question4>
    </Survey>
  </Activity>
</Contact>
</DataFile>

I need to generate to flat file.
Atleast I am expecting 2 or 3 columns:

Code:
 
 ID     subSource                        Email
1      ES 2012 BC  22461            morga@msn.com
2      ES 2012 Marketing 22402   gursingh@gmail.com


Help me guys....
I don't have advanced pgms installed. I have awk, gawk commands.

Thanks.
Sponsored Links
    #2  
Old Unix and Linux 04-19-2012
mayursingru's Unix or Linux Image
mayursingru mayursingru is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 9 February 2014, 12:12 AM EST
Location: Pune
Posts: 39
Thanks: 1
Thanked 5 Times in 5 Posts
Hi,
Try this for removing the tags and then continue with awk, gawk.


Code:
sed -e 's/<[^>]*>//g' test1.txt

Sponsored Links
    #3  
Old Unix and Linux 04-19-2012
karumudi7 karumudi7 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 22 February 2015, 9:33 PM EST
Location: India
Posts: 111
Thanks: 19
Thanked 1 Time in 1 Post
Plz help me in this. i tried but I was strucked! Unix or Linux Image
    #4  
Old Unix and Linux 04-20-2012
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 13 March 2015, 8:00 AM EDT
Location: ./India/Bangalore
Posts: 1,231
Thanks: 57
Thanked 283 Times in 276 Posts
Could this help you ?
I have change the sequence .....


Code:
 perl -nle 'BEGIN{print "ID\tEmail\tsubsource";} if (/Contact id(\s+=|=)(\s+\"|\")(.+?)\"/) {printf "%s\t",$3} if (/subSource(\s+=|=)(\s+"|")(.+?)\"/){printf "%s\n",$3} if(/<Email>(.+?)<\/Email>/){printf "%s\t",$1}'  filename

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Converting the date format in a flat file av_sagar UNIX for Advanced & Expert Users 0 05-07-2011 05:43 AM
Converting a flat file in XML humkhn Shell Programming and Scripting 4 10-11-2010 04:36 PM
Converting Column to Rows in a Flat file srinikal Shell Programming and Scripting 5 10-10-2008 03:32 PM
compare XML/flat file with UNIX file system structure shafi2all Programming 6 08-15-2008 02:15 AM
Converting Pivot file to flat file vskr72 Shell Programming and Scripting 2 10-18-2005 04:41 PM



All times are GMT -4. The time now is 07:46 PM.