Computing.Net > Forums > Programming > Merging two text files, alternating order.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Merging two text files, alternating order.

Reply to Message Icon

Name: Vance
Date: June 3, 2009 at 22:01:40 Pacific
OS: Windows Vista
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: June 3, 2009 at 22:14:53 Pacific
Reply:

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



0

Response Number 2
Name: Vance
Date: June 3, 2009 at 22:39:24 Pacific
Reply:

That worked like a charm. Thank you so much!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Merging two text files, alternating order.

Compare two text files with spaces www.computing.net/answers/programming/compare-two-text-files-with-spaces/19886.html

VB Script Compare two Text files for Duplicat www.computing.net/answers/programming/vb-script-compare-two-text-files-for-duplicat/20247.html

Remove blank box from text file www.computing.net/answers/programming/remove-blank-box-from-text-file/19485.html