Easiest scripting method to merge two text files - Ruby, Python, JavaScript, Java?

前端 未结 6 812
一个人的身影
一个人的身影 2021-01-14 21:14

I have two text files, one containing HTML and the other containing URL slugs:

FILE 1 (HTML):

  • 6条回答
    •  暗喜
      暗喜 (楼主)
      2021-01-14 21:54

      You need zip-function, which is available in most languages. It's purpose is parallel processing of two or more arrays.
      In Ruby it will be something like this:

      f1 = File.readlines('file1.txt')
      f2 = File.readlines('file2.txt')
      
      File.open('file3.txt','w') do |output_file|
      
          f1.zip(f2) do |a,b|
              output_file.puts a.sub('/article/','/article/'+b)
          end
      
      end
      

      For zipping more, than two arrays you can do f1.zip(f2,f3,...) do |a,b,c,...|

    提交回复
    热议问题