Reading Xml file and print the values into the text file in columnwise?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading Xml file and print the values into the text file in columnwise?
# 1  
Old 07-22-2015
Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file

Code:
<sequence>
<Filename>aldorzum.doc</Filename>
<DivisionCode>US</DivisionCode>
<ContentType>Template</ContentType>
<ProductCode>VIMZIM</ProductCode>
</sequence>
<sequence>
<Filename>alozyme.doc</Filename>
<DivisionCode>CA</DivisionCode>
<ContentType>Standard</ContentType>
<ProductCode />
</sequence>
<sequence>
<Filename>ASER.doc</Filename>
<DivisionCode>SA</DivisionCode>
<ContentType>Template</ContentType>
<ProductCode>ENCLOSER</ProductCode>
</sequence>

I want Out put in sample.text file below manner

Code:
Filename         ,DivisionCode      ,Product Code      ,ContentType
aldorzum.doc     , US                    ,VIMZIM           ,Template
alozyme.doc       ,CA                    ,                      ,Standard
ASER.doc          , SA                  , ENCLOSER         ,Template

I WANT ABOVE OUT PUT IN TEXT FILE WHILE READING XML FILE USING LINUX SHELL SCRIPT

PLEASE ANY ONE HELP ME...I NEED URGENT THAT LINUX SHELL SCRIPT
FOR READING XML FILE AND PRINT THE VALUES INTO THE TEXT FILE COLUMN WISE...
IF PRODUCT CODE VALUE IS EMPTY OR NOT EXIST JUST PRINT EMPTY SPACE.


i used below script not working according to my requirement.

Code:
$ nawk -F"[\"<>]" '/Filename/{id=$3}/DivisionCode/{m=$3}/ProductCode/{mc=$3}/ContentType/{ct=$3} {if(id && m && mc && ct){print id,m,mc,ct; id=m=mc=ct=0;}}' DocumentExportMetaData_20150705T000008.xml >sample.txt

but i get belowoutput
Code:
aldorzum.doc      US                    VIMZIM           Template
alozyme.doc       CA                    
                      Standard
ASER.doc           SA                   ENCLOSER         Template
pleasure.doc       CT    
                      TEMPLATE

I GOT ABOVE RESULTS WHILE USING MY SHELL SCRIPT..HERE IF PRODUCTCODE VALUE IS NOT EXIST THEN CONTENTTYPE VALUE IS PRINTED IN NEXT FILE ...
BUT I NEED IF PRODUCT CODE VALUE IS NOTEXIST OR NULL VALUE THEN LEAVE SPACE INSTEAD OF PRODUCTCODE PLACE THEN PRINT CONTENTTYPE IN NEXT COLUMN


Please any one help me...for my required output

Last edited by Don Cragun; 07-22-2015 at 09:27 PM.. Reason: Add CODE tags.
# 2  
Old 07-22-2015
You weren't too far off, try
Code:
awk -F"[\"<>]" '/Filename/{id=$3}/DivisionCode/{m=$3}/ProductCode/{mc=$3}/ContentType/{ct=$3} /\/sequence/ {print id,m,mc,ct; id=m=mc=ct=0}' OFS=, file
aldorzum.doc,US,VIMZIM,Template
alozyme.doc,CA,,Standard
ASER.doc,SA,ENCLOSER,Template

# 3  
Old 07-23-2015
xml file reading to text file using linux shell script

Thanks for RUDIC....Your code also print the values in the text file below manner
aldorzum.doc,US,VIMZIM,Template
alozyme.doc,CA,
,Standard
ASER.doc,SA,ENCLOSER,Template


but i need below out put
if product code is not exist then print space instead of product code value location.
aldorzum.doc,US,VIMZIM,Template
alozyme.doc,CA, ,Standard
ASER.doc,SA,ENCLOSER,Template

---------- Post updated at 01:02 AM ---------- Previous update was at 12:59 AM ----------

Quote:
Originally Posted by sravanreddy
Thanks for RUDIC....Your code also print the values in the text file below manner
aldorzum.doc,US,VIMZIM,Template
alozyme.doc,CA,
,Standard
ASER.doc,SA,ENCLOSER,Template


but i need below out put
if product code is not exist then print space instead of product code value location.
aldorzum.doc,US,VIMZIM,Template
alozyme.doc,CA, ,Standard
ASER.doc,SA,ENCLOSER,Template
# 4  
Old 07-23-2015
Then try:
Code:
nawk -F"[\"<>]" '
BEGIN		{id=m=mc=ct=" "}
/\/Filename/	{id=$3}
/\/DivisionCode/{m=$3}
/\/ProductCode/	{mc=$3}
/\/ContentType/	{ct=$3}
/\/sequence/	{print id,m,mc,ct;id=m=mc=ct=" "}' OFS=, file

# 5  
Old 07-23-2015
Thanks a lot Don Cragun...This code is working exactly..........thank you...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

4. Shell Programming and Scripting

Comparing delta values of one xml file in other xml file

Hi All, I have two xml files. One is having below input <NameValuePair> <name>Daemon</name> <value>tcp:7474</value> </NameValuePair> <NameValuePair> <name>Network</name> <value></value> </NameValuePair> ... (2 Replies)
Discussion started by: sharsour
2 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

7. Shell Programming and Scripting

Help with reading file with values and updating another file

Hi I need some help with a task, i am an absolute newbie to any form of shell scripting and request guidance. Task: 1. Read a config file in form of name value pair ex host=www.test.com port=8080 binding="dynamic" or "other" or "something else" key1=value1 key2=value2 key3=value4... (4 Replies)
Discussion started by: mk7074
4 Replies

8. Shell Programming and Scripting

Comparing files columnwise and print the differences in third file

Hello Everybody!!!!!!!!! Request you to help me with the below mentioned issue: I have 2 files say, File 1: a|4|7 b|3|2 c|8|8 d|8|9 File 2: a|4|6 b|2|2 c|8|8 d|9|8 The third file(output file) should have: Data mismatch in row 1 column 3 Data mismatch in row 2 coumn 2 Data... (3 Replies)
Discussion started by: abhijeet1409
3 Replies

9. Shell Programming and Scripting

Need help reading XML file

If someone out there could help me out with this problem. I would really appreciate it. My requirement is :Read XML and put data in Test.txt file.Using Shell script(bourn) My xml file: <root> <header> <HeaderData1>header1</HeaderData1> <HeaderData2>header2</HeaderData2>... (0 Replies)
Discussion started by: ram2s2001
0 Replies
Login or Register to Ask a Question