How To Find Length of a Field in XML File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How To Find Length of a Field in XML File
# 1  
Old 12-02-2008
How To Find Length of a Field in XML File

Hi

I have a xml file with below data...have to find the length of the filedvalues...


<?xml version="1.0" encoding="ISO-8859-15" standalone="no"?><abc xmlns:xsi="http://www.w3.org/2000/XMLSchem
a-instance"><FileHeader><FileName>VertSvcsDSL20081202103929.XML</FileName><NumOrderRecs>27</NumOrderRecs><NumDetailRecs>46</NumDetailRecs></FileHeader><OrderHeaderList><Order><CRMOrderID>0</CRMOrderID><CRMSvcOrderID>0</CRMSvcOrderID><UDCRMSONum>0</UDCRMSONum>
filename , NumOrderRecs and NumDetailRecs ... are field names

so need length of the data in between tags for ex: length of VertSvcsDSL20081202103929.XML

any help in this

Last edited by naughty21; 12-04-2008 at 02:04 PM..
# 2  
Old 12-02-2008
OK - can only give you a general type reply - this will require AWK.

Make your RS=">", that will break the input into lines containing the different fields.
Then you would check to see if it is a start tag or or end tag field indicator by the last character being a "/" -

When you fine a start-tag, the next record is your input value for that tag - unless it is an end-tag.

Some of the things to be carefull for in XML

It could be unicode based - what will you do then - awk and most UNIX utilities are bad at handling binary zeroes.

I have seen XML files where it is a 25000 character single line. Perfectly leagal to XML, bad for vi on UNIX. That is the reason for the Field Separator definition to awk.

in you example - filename looks like it contains two elements, and not other data.
# 3  
Old 12-02-2008
hi, not very sure what's your defination for field.

And also you are expecting length of filedname or fieldvalue.

Anyway, below may help you a little.

input:
Code:
<FileName>VertSvcsDSL20081202103929.XML</FileName><NumOrderRecs>27</NumOrderRecs><NumDetailRecs>46</NumDetailRecs>

output:
Code:
FileName -- VertSvcsDSL20081202103929.XML -- FileName
NumOrderRecs -- 27 -- NumOrderRecs
NumDetailRecs -- 46 -- NumDetailRecs

code:
Code:
open FH,"<b.txt";
my @arr=<FH>;
close FH;
foreach(@arr){
	while(m/<(.*?)>(.*?)<\/\1>/){
		print $1," -- ",$2," -- ",$1,"\n";
		$_=$';
	}
}

# 4  
Old 12-03-2008
Quote:
Originally Posted by summer_cherry
hi, not very sure what's your defination for field.

And also you are expecting length of filedname or fieldvalue.

Anyway, below may help you a little.

input:
Code:
<FileName>VertSvcsDSL20081202103929.XML</FileName><NumOrderRecs>27</NumOrderRecs><NumDetailRecs>46</NumDetailRecs>

output:
Code:
FileName -- VertSvcsDSL20081202103929.XML -- FileName
NumOrderRecs -- 27 -- NumOrderRecs
NumDetailRecs -- 46 -- NumDetailRecs


code:
Code:
open FH,"<b.txt";
my @arr=<FH>;
close FH;
foreach(@arr){
	while(m/<(.*?)>(.*?)<\/\1>/){
		print $1," -- ",$2," -- ",$1,"\n";
		$_=$';
	}
}

i need to find out the fieldvalue length means data length which is in between tags

Last edited by naughty21; 12-04-2008 at 02:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to add length of matching characters between field in file

The awk below produces the current output, which will add +1 to $3. However, I am trying to add the length of the matching characters between $5 and $6 to $3. I have tried using sub as a variable to store the length but am not able to do so correctly. I added comments to each line and the... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Find max length of the field and then replace zero

hai guys, pick the 1st field and calculate max length. if suppose max length is 2, then compare the all records if <2 then add zero's prefix of the record. for ex: s.no,sname 1,djud 37,jtuhe in this max length of the 1st field is 2 right the output wil be s.no,sname 01,djud... (6 Replies)
Discussion started by: Suneelbabu.etl
6 Replies

3. Red Hat

How to get a particular field from xml file?

i have a xml file and in that input file path is given. how to fetch that input file path using shell script. (4 Replies)
Discussion started by: ramsavi
4 Replies

4. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

5. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

6. UNIX for Dummies Questions & Answers

Extract Field Value from XML file

Hi, Within a UNIX shell script I need to extract a value from an XML field. The field will contain different values but will always be 6 digits in length. E.g.: <provider-id>999999</provider-id> I've tried various ways but no luck. Any ideas how I might get the provider id (in this case... (2 Replies)
Discussion started by: pnclayt11
2 Replies

7. Shell Programming and Scripting

Regular expression to find the length of a field

Hi, in the cobol copy books is there any regular expressions to be used in awk to fetch the length of each columns? below mentioned are the examples. Copy Book Sample 01 tablename. 02 group header. 03 col1 s9(10)V99. 03 Col2 s9(10)V9(3). 03 Col3 XXXX 02... (7 Replies)
Discussion started by: ahmedwaseem2000
7 Replies

8. Programming

Extracting Field values for XML file

i have an input file of XML type with data like <nx-charging:additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-charging:process> i want to extract data such that i get the output... (3 Replies)
Discussion started by: junaid.nehvi
3 Replies

9. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

10. Shell Programming and Scripting

convert XML file into Text file(fixed length)

If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert xml into text file(fixed length) using Unix Borne shell scripts. My xml file: <root> <header_rec recordtype="00"> <record_id>00</record_id> ... (0 Replies)
Discussion started by: ram2s2001
0 Replies
Login or Register to Ask a Question