Skip to content Skip to sidebar Skip to footer

Easiest Scripting Method To Merge Two Text Files - Ruby, Python, JavaScript, Java?


Solution 2:

This will be easy in any language. Here it is in pseudo-Python; I've omitted the lxml bits because I don't have access to them and I can't quite remember the syntax. They're not difficult, though.

with open(...) as htmls, open(...) as slugs, open(...) as output:
    for html, slug in zip(htmls, slugs):
        root = lxml.etree.fromstring(html)
        # do some fiddling with lxml to get the name

        slug = slug.split("-")[(len(name.split()):]
        # add in the extra child in lxml

        output.write(root.tostring())

Interesting features:

  • This doesn't read in the entire file at once; it does it chunk by chunk (well, line-by-line but Python will buffer it). Useful if the files are huge, but probably irrelevant.

  • lxml may be overkill, depending on how rigid the format of the html strings is. If they're guaranteed to be the same and all well-formed, it might be easier for you to use simple string operations. On the other hand, lxml is pretty fast and offers a lot more flexibility.


Solution 3:

Ruby one liner:

File.open("joined.txt","w") { |f| f.puts ['file1.txt', 'file2.txt'].map{ |s| IO.read(s) }}

Solution 4:

The easiest way to do this is to use the language of the listed ones that you are most familiar with. Even if it doesn't produce the neatest solution, you'll get the job done with the least (mental) effort.

If you know none of them, then Perl is a good option because this is the kind of thing it was designed to do. (I'm assuming that you understand regular expressions ...) And by the look of some of the other answers, Python is a good option too.


Solution 5:

Python is great language Just have a look at these six lines of python they can merge any big text file, just now i have merged 2 text file of 10 GB each.

 o = open("E:/temp/3.txt","wb") #open for write
 for line in open("E:/temp/1.txt","rb"):
     o.write(line)
 for line in open("E:/temp/2.txt","rb"):
     o.write(line)
 o.close()

Post a Comment for "Easiest Scripting Method To Merge Two Text Files - Ruby, Python, JavaScript, Java?"