Community
    • 登入

    I want to merge the content of two files but I don't know if it's possible, Please help

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    7 貼文 4 Posters 1.6k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • darklinkpowerD
      darklinkpower
      最後由 編輯

      I have two files that differ in content and I want to make a third one that combines both but I don’t know if it’s possible. The lines look something like this:

      File A:

      sametext
      <text><![line a[line b, line c, line d, A1, A2, A3]]></text>
      sametext
      

      File B:

      sametext
      <text><![line a[line b, line c, line d, B1, B2, B3]]></text>
      sametext
      

      And I want to make a third file that looks like this:

      sametext
      <text><![line a[line b, line c, line d, A1, A2, A3, B1, B2, B3]]></text>
      sametext
      

      Forgive me if this is an easy task but sadly I don’t have the knowledge to do it so I figured I could ask here if it’s possible. Thanks in advance.

      1 條回覆 最後回覆 回覆 引用 0
      • darklinkpowerD
        darklinkpower
        最後由 darklinkpower 編輯

        Said text files contain hundreds of lines like this so doing it manually would be very difficult, that’s why I’m looking if there can be a way to do it with n++. I also forgot to add that the lines differ slightly, changing the content of the different line
        for example:

        <text><![line a[word, ads, wdwd , wsws, dfdf, dff]]></text>
        

        For the next line, while it follows the same structure, changes in the content after “line a[” but end in the same "]]></text> ". So basically what I want is a way to fuse the differences together of the lines that differ but I don’t know if it’s possible since there’s not a clear pattern other than the estructure of the lines

        1 條回覆 最後回覆 回覆 引用 0
        • rinku singhR
          rinku singh
          最後由 rinku singh 編輯

          replace ]]></text> with ,
          after do as given suggest
          https://www.youtube.com/embed/1g8sTZ85Bgg

          after replace $ with ]]></text>

          1 條回覆 最後回覆 回覆 引用 0
          • PeterJonesP
            PeterJones
            最後由 編輯

            @darklinkpower,

            This sounds very similar to the recent “How to merge” topic. You may be able to use or modify-for-use the regex solution solution that @guy038 posted or the PythonScript solution that I posted. They may have to be tweaked slightly for your exact circumstances, but they are a good starting point.

            If you get stuck while trying to modify them to meet your needs, let us know what you’ve tried, and what parts are giving you difficulty, and we may be able to help you get unstuck.

            1 條回覆 最後回覆 回覆 引用 0
            • darklinkpowerD
              darklinkpower
              最後由 編輯

              thanks for the help guys
              @gurikbal-singh nice idea, I didn’t know you could select multiple lines that way. Please correct me if I’m wrong but this could only work if the line lenghts are in the same place right? because if the text that I want to copy don’t align then I will copy text that I don’t want to

              @PeterJones I don’t think it’s the same issue. There he has 2 list that he wants to merge, without any extra data that he wants to get rid of. What I want to do is just copy some data of the second file, not all.
              After googling around I found what you can use expressions to match text and I came up with this that matches the text I want to copy to the other file, but I don’t know how to copy it to the other file. The files are xml files

              Here’s what I could come with:
              https://regex101.com/r/xR5PG4/2

              1 條回覆 最後回覆 回覆 引用 1
              • PeterJonesP
                PeterJones
                最後由 PeterJones 編輯

                @darklinkpower said:

                I don’t think it’s the same issue.

                I didn’t say it was the same. I said it sounds similar. Good job on coming up with a regex that gets you partway there.

                The problem you see now is that it looks like more of a problem than a simple regex will solve. (That said, @guy038 can work magic with his regex and multi-step regex.)

                But at first blush, the problem you describe looks to me to be something best solved using programming, rather than being able to solve it with a simple regular expression – which is why I pointed out the PythonScript solution for the other merge-based issue. One possible way of doing this is using Notepad++'s PythonScript plugin, which embeds a Python interpreter inside a Notepad++ plugin environment, allowing you to write a script where Python can directly read and write the open Notepad++ files. (You can do something similar with Lua using LuaScript Plugin, or JavaScript using “jN Notepad++ Plugin”.)

                I had meant for the script linked in the other topic to give you a starting point, because I had thought I’d used just normal line-by-line reading in one and writing in the other. But since it wants to merge every line in the second onto N copies of every line in the first, I used some tricks, which eliminated the logic which would have helped you; sorry.

                If you can code in Python (or Lua, or JavaScript), you could probably come up with an algorithm. The online help for PythonScript is very detailed.

                Off the top of my head, the top-level description would be “for each of the matches in the second file (and PythonScript has access to a similar regular expression library, so finding those matches won’t be too difficult), grab the right portion of the matching text, and insert it into the correct line of the first file.” Implementing that in more detail is what programmers get paid for. For some problems, one of us in the Community occasionally writes the script for free (usually if we’re interested in the problem or see that we can apply it generally to a task that we have to do as well, or if it looks like we could get it in a brief amount of time), but that doesn’t always happen (this isn’t a we-guarantee-we’ll-write-your-code-for-free forum, after all). And this one looks to me to be a longer problem than I’m likely to have time for in the forseeable future. Maybe it’ll catch someone else’s eye, though.

                if you were to come up with a rough attempt of a PythonScript solution, that got close but not exactly what you wanted, and posted it here, there might be more people who would have the time to help point you in the right direction, or show you what Notepad+±specific features of PythonScript that could help.

                Sorry that I don’t offer more of a solution, but while Notepad++ is powerful, there are some things that are better handled with actual programming, and you may have hit one (unless @guy038 proves me wrong).

                1 條回覆 最後回覆 回覆 引用 1
                • guy038G
                  guy038
                  最後由 guy038 編輯

                  Hello, @darklinkpower,

                  I was away from the N++ community, since more than a month ( see my post below )

                  https://community.notepad-plus-plus.org/post/47895

                  So, @darklinkpower, my reply is quite out of date. But if you still look for a regex solution I can take up the challenge of performing this S/R with regexes !

                  However you must give me a real example of your 2 ( short ) files A and B, as well as the expected real output file, from these 2 input files

                  Simply, as your files are XML ones, in your future post, write your code in blocks, like below :

                  ~~~xml
                  …
                  …
                  ~~~


                  Just to give you an idea, of the regexes’s powerful, let’s suppose the input text, below :

                  <text><![line a[line b, line c, line d, A1, A2, A3]]></text>
                  <text><![line a[line b, line c, line d, B1, B2, B3]]></text>
                  
                  <text><![line a[line b, line c, line d, line e, line f, line g, line h, C1, C2, C3, C4]]></text>
                  <text><![line a[line b, line c, line d, line e, line f, line g, line h, D1, D2, D3, D4]]></text>
                  
                  <text><![line a[Small test, E1, E2, E3, E4, E5, E6, E7, E8, E9]]></text>
                  <text><![line a[Small test, F1, F2, F3, F4, F5, F6, F7, F8, F9]]></text>
                  
                  <text><![line a[word 1, G1, G2]]></text>
                  <text><![line a[word 1, H1, H2]]></text>
                  
                  <text><![line a[I1]]></text>
                  <text><![line a[J1]]></text>
                  

                  Then, the following regex S/R :

                  SEARCH (?-s)^(.+)(.+)\]\]></text>(\R)\1(.+)\R

                  REPLACE \1\2, \4\3

                  will change, when you click on the Replace All button, in one go, the text as below :

                  <text><![line a[line b, line c, line d, A1, A2, A3, B1, B2, B3]]></text>
                  
                  <text><![line a[line b, line c, line d, line e, line f, line g, line h, C1, C2, C3, C4, D1, D2, D3, D4]]></text>
                  
                  <text><![line a[Small test, E1, E2, E3, E4, E5, E6, E7, E8, E9, F1, F2, F3, F4, F5, F6, F7, F8, F9]]></text>
                  
                  <text><![line a[word 1, G1, G2, H1, H2]]></text>
                  
                  <text><![line a[I1, J1]]></text>
                  

                  As you can see, this regex S/R is able to differentiate the first common part of two consecutive lines from the second variable part which must be merged in one line only ;-))

                  So, see you later !

                  Best Regards,

                  guy038

                  1 條回覆 最後回覆 回覆 引用 1
                  • 第一個貼文
                    最後的貼文
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors