Merging two text files, alternating order.

June 3, 2009 at 22:01:40
Specs: Windows Vista
Hi,

I'm having trouble merging two text files. I need to merge them so that the lines alternate, eg.

doc1line1
doc2line1
doc1line2
doc2line2

etc.

Is there any way to do this?
Thanks.


See More: Merging two text files, alternating order.

Report •


#1
June 3, 2009 at 22:14:53
assuming the 2 files are of same number of lines, and if you have Python on Windows
f1=open("file1")        
f2=open("file2")
for line in f1:
    print line.strip()
    print f2.readline().strip()
f1.close()
f2.close()

output
c:\test> more file1
file1 line1
file1 line2
file1 line3
c:\test> more file2
file2 line1
file2 line2
file2 line3
c:\test> python test.py
file1 line1
file2 line1
file1 line2
file2 line2
file1 line3
file2 line3



Report •

#2
June 3, 2009 at 22:39:24
That worked like a charm. Thank you so much!

Report •

Related Solutions


Ask Question