How to merge two files like the following with the XML or text parser

K

karenmiddleol

I have two text files.

I want to merge them together as follows:

================================
File1.txt

SCREEN,
PROGRAM,
FIELD1,
VALUE1,
FIELD2,
VALUE2,
FIELD3,
VALUE3,
================================


================================
File2.txt


S1,P1,F11,V11,F12,V12,F13,V13
S2,P2,F21,V21,F22,V22,F23,V23
....
....

================================

Now when I merge the two files together I want the following file:

================================
File3.txt


SCREEN, S1
PROGRAM,P1
FIELD1,F11
VALUE1,V11
FIELD2,F12
VALUE2,V12
FIELD3,F13
VALUE3,V13
SCREEN, S2
PROGRAM,P2
FIELD1,F21
VALUE1,V21
FIELD2,F22
VALUE2,V22
FIELD3,F23
VALUE3,V23


=======================================================


I want a generic script that can merge file1.txt and file2.txt to
generate file3.txt


I want a script that can take a template of File1.txt as follows:


SCREEN, <<S>>
PROGRAM, <<P>>
FIELD1, <<C1>>
VALUE1, <<C2>>
FIELD2, <<C3>>
VALUE2, <<C4>>
FIELD3, <<C5>>
VALUE3, <<C6>>


=======================================================


and the file2.txt to identify what goes to <<S>>, <<P>>
in the following form:

================================
File2.txt
<<P>>,<<S>>,<<C1>>,<<C2>>,<<C3>>,<<C4>>,<<C5>>,<<C6>>
S1,P1,F11,V11,F12,V12,F13,V13
S2,P2,F21,V21,F22,V22,F23,V23
....
....

=======================================================


So I want a generic XML or text parser based utility that can accept
three arguments for file1, file2 and file3 and can merge the template
of file1 to the contents in file2 to be substituted in file1 to
generate file3.


Appreciate if any of you can kindly share code to get the XML or text
parser to do this which most of you would have faced with such a
similar problem.

It is almost like a mail merge but the data volumes can be large and it
is not practical to use MS Word.

To keep it simple I want the whole merge process in XML or text parser.

Appreciate if you could kindly share how to get this done with the XML
or text parser a generic parsing and merging like the above


Thanks
Karen
 
U

usenet

I want... I want... I want... I want...

I want a million bucks.
Appreciate if you could kindly share how to get this done with the XML
or text parser...

We'll be glad to help you, but we're not gonna write your program for
you. What have you tried so far, and what problems have you run into?

Before you reply, I recommend you read the posting guidelines for this
group:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
R

robic0

I have two text files.

I want to merge them together as follows:

================================
File1.txt

SCREEN,
PROGRAM,
FIELD1,
VALUE1,
FIELD2,
VALUE2,
FIELD3,
VALUE3,
================================


================================
File2.txt


S1,P1,F11,V11,F12,V12,F13,V13
S2,P2,F21,V21,F22,V22,F23,V23
...
...

================================

Now when I merge the two files together I want the following file:

================================
File3.txt


SCREEN, S1
PROGRAM,P1
FIELD1,F11
VALUE1,V11
FIELD2,F12
VALUE2,V12
FIELD3,F13
VALUE3,V13
SCREEN, S2
PROGRAM,P2
FIELD1,F21
VALUE1,V21
FIELD2,F22
VALUE2,V22
FIELD3,F23
VALUE3,V23


=======================================================


I want a generic script that can merge file1.txt and file2.txt to
generate file3.txt


I want a script that can take a template of File1.txt as follows:


SCREEN, <<S>>

You would you be better off to have more distinct delimeters.
SCREEN said:
PROGRAM, <<P>>
FIELD1, <<C1>>
VALUE1, <<C2>>
FIELD2, <<C3>>
VALUE2, <<C4>>
FIELD3, <<C5>>
VALUE3, <<C6>>


=======================================================


and the file2.txt to identify what goes to <<S>>, <<P>>
in the following form:

================================
File2.txt
<<P>>,<<S>>,<<C1>>,<<C2>>,<<C3>>,<<C4>>,<<C5>>,<<C6>>
S1,P1,F11,V11,F12,V12,F13,V13
S2,P2,F21,V21,F22,V22,F23,V23

No matter how store or read this data, it will end
up into a hash array.

Theres no XML solution, xml will just hold the data in a
structured way to be parsed into a hash array.
ie: <P>S1</P><S>P1</S><C1>F11</C1> ....
ends up being
%hTag = (P => 'S1', S => 'P1', C1 => 'F11');
...
...

=======================================================


So I want a generic XML or text parser based utility that can accept
three arguments for file1, file2 and file3 and can merge the template
of file1 to the contents in file2 to be substituted in file1 to
generate file3.


Appreciate if any of you can kindly share code to get the XML or text
parser to do this which most of you would have faced with such a
similar problem.

It is almost like a mail merge but the data volumes can be large and it
is not practical to use MS Word.

To keep it simple I want the whole merge process in XML or text parser.

Appreciate if you could kindly share how to get this done with the XML
or text parser a generic parsing and merging like the above


Thanks
Karen

Its a simple matter of opening up the data File2, get the data into
a hash array, creating the output file File3, opening up the tag file
File1 then parse each tagfile line like this:

if (!open (ORIG, ">$newfile")) {
push (@{$referrs}, "Cannot open $newfile: $!") if defined($referrs);
return 2;
}
if (!open (TAG, "<$tagfile")) {
push (@{$referrs}, "Cannot open $tagfile: $!") if defined($referrs);
return 3;
}

# Non-recursive search
while (<TAG>)a
{
chomp;
my $addline = undef;
while (/<$tagstr([^<]+)>/)
{
# $` - string before match
# $' - string after match
my $key = $1;
# Is the tag in the hash table?
if (exists $hTags->{$key})
{
my $val = $hTags->{$key};
$addline = $addline.$`.$val;
$_ = $';
} else {
# error, tag not in hash table
push (@{$referrs}, "$key is not in table -> : $_") if
defined($referrs);
$addline = $addline.$`."<nokey_".$tagstr.$key.">";
$_ = $';
}
}
print (ORIG $addline.$_."\n");
}
close (ORIG);
close (TAG);
 
T

Tad McClellan

I have two text files.
Now when I merge the two files together I want the following file:

================================
File3.txt
SCREEN, S1
PROGRAM,P1
FIELD1,F11
VALUE1,V11
FIELD2,F12
VALUE2,V12
FIELD3,F13
VALUE3,V13
SCREEN, S2
PROGRAM,P2
FIELD1,F21
VALUE1,V21
FIELD2,F22
VALUE2,V22
FIELD3,F23
VALUE3,V23


------------------------
#!/usr/bin/perl
use warnings;
use strict;

my @file1 = split /,\n/, <<FILE1;
SCREEN,
PROGRAM,
FIELD1,
VALUE1,
FIELD2,
VALUE2,
FIELD3,
VALUE3,
FILE1

while ( <DATA> ) {
chomp;
my @fields = split /,/;
foreach my $i ( 0 .. $#fields ) {
print "$file1[$i],$fields[$i]\n";
}
}

__DATA__
S1,P1,F11,V11,F12,V12,F13,V13
S2,P2,F21,V21,F22,V22,F23,V23
 
S

Stephen O'D

I have two text files.

I want to merge them together as follows:

================================
File1.txt

SCREEN,
PROGRAM,
FIELD1,
VALUE1,
FIELD2,
VALUE2,
FIELD3,
VALUE3,
================================


================================
File2.txt


S1,P1,F11,V11,F12,V12,F13,V13
S2,P2,F21,V21,F22,V22,F23,V23
...
...

================================

Now when I merge the two files together I want the following file:

================================
File3.txt


SCREEN, S1
PROGRAM,P1
FIELD1,F11
VALUE1,V11
FIELD2,F12
VALUE2,V12
FIELD3,F13
VALUE3,V13
SCREEN, S2
PROGRAM,P2
FIELD1,F21
VALUE1,V21
FIELD2,F22
VALUE2,V22
FIELD3,F23
VALUE3,V23


=======================================================


I want a generic script that can merge file1.txt and file2.txt to
generate file3.txt


I want a script that can take a template of File1.txt as follows:


SCREEN, <<S>>
PROGRAM, <<P>>
FIELD1, <<C1>>
VALUE1, <<C2>>
FIELD2, <<C3>>
VALUE2, <<C4>>
FIELD3, <<C5>>
VALUE3, <<C6>>


=======================================================


and the file2.txt to identify what goes to <<S>>, <<P>>
in the following form:

================================
File2.txt
<<P>>,<<S>>,<<C1>>,<<C2>>,<<C3>>,<<C4>>,<<C5>>,<<C6>>
S1,P1,F11,V11,F12,V12,F13,V13
S2,P2,F21,V21,F22,V22,F23,V23
...
...

=======================================================


So I want a generic XML or text parser based utility that can accept
three arguments for file1, file2 and file3 and can merge the template
of file1 to the contents in file2 to be substituted in file1 to
generate file3.


Appreciate if any of you can kindly share code to get the XML or text
parser to do this which most of you would have faced with such a
similar problem.

It is almost like a mail merge but the data volumes can be large and it
is not practical to use MS Word.

To keep it simple I want the whole merge process in XML or text parser.

Appreciate if you could kindly share how to get this done with the XML
or text parser a generic parsing and merging like the above


Thanks
Karen

Well as I replied in your post in the Oracle groups:-

Also, I don't know the background of the problem, but do you really
need files 1 and 3? Could you not format file 2 as follows:-


SCREEN,PROGRAM,FIELD1,VALUE1,FIELD2,VALUE2
S1,P1,F1,V1,F2,V2
S1,P1,F1,V1,F2,V2
S1,P1,F1,V1,F2,V2
....


ie, file two contains on the first line the name of each field and each

subsequent line contains the value for each field. This is effectively

what file 3 is going to be anyway, only layed out differently, only
this way you need 1 file and don't need to merge them!
 
R

robic0

(e-mail address removed) wrote:
We'll be glad to help, but we're not gonna write your program for you.

Tad McClellan wrote:
[the program]

Hmmmm. I stand corrected.
You have to get off your ass before you can stand.
Simple physics.

How about you shut up your noisy trap a bit, okay?

Anno
You are correct. My appologies to David.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top