Converting Text File into XML using Unix Shell Scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting Text File into XML using Unix Shell Scripts
# 1  
Old 02-15-2005
Question Converting Text File into XML using Unix Shell Scripts

Hi everyone,

If someone out there could help me out with this problem. I would really appreciate it.

I am trying to convert a file into xml format using Unix shell scripts.

The file has fields with each field having a certain number of bytes, but the fields are not delimited by anything (e.g. whitespace). I need to get those fields into some sort of data structure so that I can use them to generate the XML using a simple for loop. The following is a an example of what I am looking at:

Jack Johnson 90980288Harv 9090998
Joe Joie 8989 Sed 99488


I can't use whitespace as a delimiter.


Thanks you guys very much,
I really appreciate it.
# 2  
Old 02-15-2005
Computer

What kind of data structure do you require?
You can make it comma delimited by executing:

sed 's/ /,/g' inputfile.list > outputfile.lst
this will convert all whitespaces of inputfile.lst to a comma in outputfile.lst.
# 3  
Old 02-15-2005
Re:

The reason I cannot use a comma as a delimiter or anything like that is because the fields are not seperated by a delimiter, but allocated a certain number of bytes.

For example:

Firstname (10) Last Name (10)


MickelsonJackonson ( You will have fields that take up all space in the field and you won't have any commas in there)


I am thinking of using the substr function from awk which will be able to read in the exact bytes from each line and then put it into an array or something to process it.
# 4  
Old 02-15-2005
hmm similar like something I am working on myself. I need to judge the varying length of a string and create independent results. I don't think substr will work for me though, but sounds like it may be one solution for your quandary.
# 5  
Old 02-15-2005
Quote:
Originally Posted by Laud12345
The reason I cannot use a comma as a delimiter or anything like that is because the fields are not seperated by a delimiter, but allocated a certain number of bytes.

For example:

Firstname (10) Last Name (10)


MickelsonJackonson ( You will have fields that take up all space in the field and you won't have any commas in there)


I am thinking of using the substr function from awk which will be able to read in the exact bytes from each line and then put it into an array or something to process it.

if your "fields" are of fixed width you can take advantage of gawk's "FIELDWIDTH" internal variable for processing.

If you don't have gawk installed on your system, take a look at this thread at comp.lang.awk how to "simulate" FIELDWIDTH in other awks:FIELDWIDTH
# 6  
Old 02-16-2005
Thanks guys very much. I was able to use the substr function in order to read in the fixed columns. I am also considering doing the gawks' fieldwidth thing, but would that affect the performance? I am to be working on around 800,000 records so it also needs to be pretty efficient.

Also, is there is a quick function in awk that lets you strip away leading and trailing whitespace from a string? I was thinking of using the 'split' function to store the string into an array and then printing out the array, but that seems to be overkill. Any help would be appreciated.

Please let me know
Laud
# 7  
Old 02-16-2005
Code:
function trim(str)
{
    sub("^[ ]*", "", str);
    sub("[ ]*$", "", str);
    return str;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. Solaris

XML to Text file Parsing Using shell scripting

Hi, I want to parse an XML File using Shell Script preferably by using awk command, I/P file is : <gn:ExternalGsmCell id="016P3A"> <gn:attributes> <gn:mnc>410</gn:mnc> <gn:mcc>310</gn:mcc> <gn:lac>8016</gn:lac> ... (2 Replies)
Discussion started by: tech_frk
2 Replies

4. Shell Programming and Scripting

Help with converting XML to Flat file

Hi Friends, I want to convert a XML file to flat file. Sample I/p: <?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... (3 Replies)
Discussion started by: karumudi7
3 Replies

5. Shell Programming and Scripting

XML to Text file Parsing Using shell scripting

Hi folks, Need some help with XML to text file parsing , the following is the content of the XML File. <xn:SubNetwork id="SNJNPRZDCR0R03"> <xn:MeContext id="PRSJU0005"> <xn:VsDataContainer id="PRSJU0005"> <xn:attributes> ... (6 Replies)
Discussion started by: tech_frk
6 Replies

6. UNIX for Dummies Questions & Answers

converting scripts from dos 2 unix format

Hi, I am new to shell scripting and exploring it , I have developed few sample shell script but I have developed them on windows xp notepad and then saving them on folder and then testing them on cywgin and running perfectly...but these scripts are in dos format and I want to convert them in unix... (1 Reply)
Discussion started by: rahul125
1 Replies

7. Shell Programming and Scripting

Shell program to check if the same text appears twice in an XML file

Hi All, I am very new to this forum and beginner to shell scripting. I need a shell script to: Search for a text in XML file if the same text appears twice in an XML file output file name Script should loop thru every xml file of a given folder. Please help me writing this script. ... (1 Reply)
Discussion started by: amardeep001
1 Replies

8. Windows & DOS: Issues & Discussions

Converting UNIX scripts to DOS

Is there a tool available to convert UNIX (BASH Shell) scripts to DOS scripts? I understand that DOS scripting is far inferior to unix scripting, and therfore this conversion may not be possible. Alternativley, perhaps I could convert my Unix scripts to C... then compile it for a windows... (2 Replies)
Discussion started by: Crozz
2 Replies

9. Shell Programming and Scripting

Plz Help To convert xml file to text file using bourn shell scripts

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> <country_code>AK></country_code>... (0 Replies)
Discussion started by: ram2s2001
0 Replies
Login or Register to Ask a Question

Featured Tech Videos