How to merge two files like the following with the XML 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 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 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 parser.

Appreciate if you could kindly share how to get this done with the XML
parser.


Thanks
Karen
 
M

Michael Fellinger

--nextPart1604696.SXLYrWLu7W
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

My humble approach :) (just while waiting for the guys with the injects)

f1 = File.new("File1.txt").readlines
f2 = File.new("File2.txt").readlines

result = []
f2.each{|x| x.each_index{|y| result << [f1[y], x[y]]}}

puts result.inspect


so long...
manveru

Am Donnerstag 27 Oktober 2005 22:52 schrieb (e-mail address removed):
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 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 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 parser.

Appreciate if you could kindly share how to get this done with the XML
parser.


Thanks
Karen

--nextPart1604696.SXLYrWLu7W
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBDYUV/MdQeL6eBxhIRArMiAKC79ll0yNmdyb/9yvc7aJfo4RJchgCfZ44S
ao3llER5uJorDSWL/kZN03Q=
=NCnd
-----END PGP SIGNATURE-----

--nextPart1604696.SXLYrWLu7W--
 
D

daz

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


a1 = File.read(ARGV[0]).split(/\n/) # SCREEN, ...

File.open(ARGV[2], 'w') do | f3 | # output
IO.foreach(ARGV[1]) do | lin2 | # S1,P1,F11, ...
a2 = lin2.chomp.split(/,/)
a1.each_index do | ix |
f3.puts "#{a1[ix]}#{a2[ix]}"
end
end
end

Appreciate if you could kindly share how to get
this done with the XML parser.


The task is so simple, you needn't go near XML parser
but you can access that from Ruby like so:

require 'win32ole'
xmp = WIN32OLE.new('Microsoft.XMLDOM')
puts xmp.ole_methods.sort_by {|e| e.name}


Try googling: win32ole "Microsoft.XMLDOM"

Ask here if you're stuck.


daz
 

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,737
Latest member
Georgeengab

Latest Threads

Top