• Login
Community
  • Login

Replacing variable length file paths in a GEDCOM file

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
44 Posts 5 Posters 3.4k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W
    Web Master @Alan Kilborn
    last edited by Mar 3, 2022, 7:00 PM

    @alan-kilborn said in Replacing variable length file paths in a GEDCOM file:

    @web-master said in Replacing variable length file paths in a GEDCOM file:

    Something like this:

    Find: (?-s)^(1 FILE ).+/
    Replace: ${1}/Smith_1234/
    Search mode: Regular expression

    but some of the files have 1million lines!!!

    With that much data, before any important transformation operation, please back up your data!

    Wow! Got it in one! It worked.

    Mind you, the file I chose at random to test it on has a variation on the theme because instead of each line starting "1 FILE " they started "2 FILE ". But tweaking the expression fixed that. What would the syntax be for any line starting 0 - 9 then FILE, please?

    P 1 Reply Last reply Mar 3, 2022, 7:01 PM Reply Quote 0
    • P
      PeterJones @Web Master
      last edited by PeterJones Mar 3, 2022, 7:02 PM Mar 3, 2022, 7:01 PM

      @web-master said in Replacing variable length file paths in a GEDCOM file:

      What would the syntax be for any line starting 0 - 9 then FILE, please?

      \d means match a single digit (0-9); \d+ means match one or more digits (0-99999…)

      W 1 Reply Last reply Mar 3, 2022, 7:10 PM Reply Quote 1
      • W
        Web Master @PeterJones
        last edited by Mar 3, 2022, 7:10 PM

        @peterjones said in Replacing variable length file paths in a GEDCOM file:

        @web-master said in Replacing variable length file paths in a GEDCOM file:

        What would the syntax be for any line starting 0 - 9 then FILE, please?

        \d means match a single digit (0-9); \d+ means match one or more digits (0-99999…)

        OK so if the Find what becomes…

        (?-s)^\d( FILE ).+/

        … the replace with gets SNAFU’d and loses the inital number

        FILE /Smith_1234/Robert Frederic & Audrey Lilian - 3.jpg

        instead of

        2 FILE /Smith_1234/Robert Frederic & Audrey Lilian - 3.jpg

        How do I pass the initial number from the find to the replace please?

        P 1 Reply Last reply Mar 3, 2022, 7:11 PM Reply Quote 0
        • P
          PeterJones @Web Master
          last edited by PeterJones Mar 3, 2022, 7:11 PM Mar 3, 2022, 7:11 PM

          @web-master ,

          Why did you take it out of the parentheses? You had (?-s)^(1 FILE ).+/, and I suggested that you use \d instead of 1, which would have been (?-s)^(\d FILE ).+/ … if the \d is in the parentheses, it will be included in the replacement using the $1 that’s already there.

          W 1 Reply Last reply Mar 3, 2022, 9:36 PM Reply Quote 2
          • W
            Web Master @PeterJones
            last edited by Mar 3, 2022, 9:36 PM

            @peterjones I figured that 1 FILE inside the paranetheses was a literal.

            P 1 Reply Last reply Mar 3, 2022, 9:39 PM Reply Quote 0
            • P
              PeterJones @Web Master
              last edited by PeterJones Mar 3, 2022, 9:40 PM Mar 3, 2022, 9:39 PM

              @web-master ,

              I figured that 1 FILE inside the paranetheses was a literal.

              The parentheses doesn’t make it literal. The parentheses makes it a group, so that it will capture the matched text inside the parentheses as a group, which you can then reference during the replacement to get back pieces of what was in your match. By taking the \d outside the parentheses, the digit wasn’t kept as part of the group, and thus wasn’t available to be inserted into the replacement.

              • capture groups (see Numbered Capture Group in that section)
              • replacement expressions (see $ℕ in that section)
              W 1 Reply Last reply Mar 3, 2022, 10:29 PM Reply Quote 1
              • W
                Web Master @PeterJones
                last edited by Mar 3, 2022, 10:29 PM

                @peterjones Got it! Thank you

                This works beautifully. Is there a way to put this into a macro so that at runtime the user is prompted for the replacement string? That would be the cherry on top of the icing/frosting on top of the cake.

                No biggie if it’s too big an ask. I’m sure I can coach the user to do it properly

                Paul

                P A 2 Replies Last reply Mar 3, 2022, 10:33 PM Reply Quote 0
                • P
                  PeterJones @Web Master
                  last edited by Mar 3, 2022, 10:33 PM

                  @web-master ,

                  The macro language used by Notepad++ has no concept of “prompt”, so the simple answer is “no”.

                  “Or something”, OTOH: there is a PythonScript plugin, which allows you to write a script that is run inside of Notepad++ which can automate the Notepad++ GUI and editor contents, which could run that search/replace; but you’d have to install the plugin, and learn how to write such a script. It really depends on how much effort you want to go to now, to make it easier for your users to do this in the future. I think Alan’s posted at least one script in the past that pops up a user input box and asks for text, which then gets populated into a regex that is automatically run on the current file… searching the forum for posts by @Alan-Kilborn that contain prompt might get you there eventually (though you’ll probably have to wade through quite a few false hits as well… prompt isn’t that uncommon a word).

                  W 1 Reply Last reply Mar 3, 2022, 10:58 PM Reply Quote 1
                  • W
                    Web Master @PeterJones
                    last edited by Mar 3, 2022, 10:58 PM

                    @peterjones Thanks. I’ll try coaching the user first. :)

                    I am so impresssed with the speed and quality of response to my post. Thank you so much Alan and Peter

                    N 1 Reply Last reply Mar 4, 2022, 3:42 AM Reply Quote 2
                    • N
                      Neil Schipper @Web Master
                      last edited by Neil Schipper Mar 4, 2022, 3:44 AM Mar 4, 2022, 3:42 AM

                      @web-master Hi. Here’s a variation on Alan’s solution which has the feature that users enter the replacement text as is (so they won’t have to be so careful about entering text into a field containing cryptic regex codes) : (?<=^\d FILE /).*(?=/)

                      (Edit: slightly simplified)

                      W 1 Reply Last reply Mar 4, 2022, 10:44 AM Reply Quote 2
                      • W
                        Web Master @Neil Schipper
                        last edited by Mar 4, 2022, 10:44 AM

                        @neil-schipper said in Replacing variable length file paths in a GEDCOM file:

                        (?<=^\d FILE /).*(?=/)

                        Forgive me Neil, but I’m not clear how to use this modified example?

                        N 1 Reply Last reply Mar 4, 2022, 11:06 AM Reply Quote 0
                        • N
                          Neil Schipper @Web Master
                          last edited by Mar 4, 2022, 11:06 AM

                          @web-master I could have been more clear.

                          The idea is that you perform a Regex search in the same way as the earlier recipe, except the Find expression is the one I offered, and, the Replace expression is simply and completely the text you want to substitute in.

                          Thus, instead of your people having to carefully modify the Replace expression from ${1}/Smith_1234/ to ${1}/Jones_4567/ as they proceed from one task to the next, they can more comfortably change Smith_1234 to Jones_4567 – no funny confusing text in the Replace text box.

                          Also, if you want the solution to be robust against users inadvertently checking that option box to the right of Reg Exp, prefix the Find expression with (?-s) as per Alan’s earlier expression.

                          Also, if you want to make it impossible to match a completely empty path (nothing between two slashes: //), in my Find expression, we’d replace * with +, hence: (?-s)(?<=^\d FILE /).+(?=/)

                          W A 2 Replies Last reply Mar 4, 2022, 12:05 PM Reply Quote 1
                          • W
                            Web Master @Neil Schipper
                            last edited by Mar 4, 2022, 12:05 PM

                            @neil-schipper Hi Neil. OK, that’s what I thought you meant, but when I do that, Find and Replace does nothing

                            N 1 Reply Last reply Mar 4, 2022, 12:17 PM Reply Quote 0
                            • A
                              Alan Kilborn @Neil Schipper
                              last edited by Mar 4, 2022, 12:16 PM

                              @neil-schipper said in Replacing variable length file paths in a GEDCOM file:

                              Also, if you want to make it impossible to match a completely empty path (nothing between two slashes: //), in my Find expression, we’d replace * with +

                              I’m surprised you changed (my) original usage of .+ to .* in your version, then backed it out with “we’d replace * with +”. :-)

                              N 1 Reply Last reply Mar 4, 2022, 12:33 PM Reply Quote 0
                              • N
                                Neil Schipper @Web Master
                                last edited by Mar 4, 2022, 12:17 PM

                                @web-master

                                Just to be clear:
                                Find: (?-s)(?<=^\d FILE /).+(?=/)
                                Replace: Smith_1234
                                Mode=regex; checkbox to the right doesn’t matter anymore

                                Then, Find Next to observe expected matching, Replace to perform the substitution on the currently selected match, Replace All to perform the substitution on entire file.

                                I have tested this. Are you sure you aren’t acting on a file that has already had the substitutions done?

                                W 1 Reply Last reply Mar 4, 2022, 1:58 PM Reply Quote 0
                                • N
                                  Neil Schipper @Alan Kilborn
                                  last edited by Mar 4, 2022, 12:33 PM

                                  @alan-kilborn said:

                                  you changed (my) original usage of .+ to .*

                                  That’s not how it actually went. I devised my solution independently from reading the OP (deciding early that it should have a look-behind and a look-ahead). Then I saw you had a solution that was satisfactory, and I saw no reason to pipe in.

                                  Much later I noticed in the follow-up convo that @web-master was trying to reduce complexity for his non-technical volunteers, and realized my solution was favorable to that. And it was after that that I realized our solutions also differed in the modifier, and that the asterisk was maybe a bit too loose (although, dollars to doughnuts, they perform equally).

                                  1 Reply Last reply Reply Quote 1
                                  • A
                                    Alan Kilborn @Web Master
                                    last edited by Alan Kilborn Mar 4, 2022, 12:46 PM Mar 4, 2022, 12:44 PM

                                    @web-master said in Replacing variable length file paths in a GEDCOM file:

                                    Is there a way to put this into a macro so that at runtime the user is prompted for the replacement string?

                                    As @PeterJones mentioned, not possible with a macro.

                                    But as a PythonScript it is rather simple:

                                    replacement_text = notepad.prompt('Replacement text:', '', '')
                                    if replacement_text:
                                        find_regex = r'(?-s)(?<=^\d FILE /).+(?=/)'
                                        editor.rereplace(find_regex, replacement_text)
                                    

                                    You’d run the script and then get prompted with:

                                    a7afa474-b454-42b6-82e0-47d305e365b2-image.png

                                    After pressing OK the replacements would be made.

                                    1 Reply Last reply Reply Quote 2
                                    • W
                                      Web Master @Neil Schipper
                                      last edited by Mar 4, 2022, 1:58 PM

                                      @neil-schipper Yes, I copied and pasted the expression direct from here and the file had def not been processed already.

                                      I don’t know what the protocol is here. May I PM you the file so you can try it?

                                      A N 2 Replies Last reply Mar 4, 2022, 2:11 PM Reply Quote 0
                                      • A
                                        Alan Kilborn @Web Master
                                        last edited by Mar 4, 2022, 2:11 PM

                                        @web-master

                                        Why don’t you just post a line – or a few lines – here that you think it should match, but it isn’t? That may be more instructive (for those reading along) than taking it offline into some private discussion.

                                        While initially impressed with your initiative, I’m now losing faith in your debugging skills. :-(

                                        W 1 Reply Last reply Mar 4, 2022, 2:27 PM Reply Quote 0
                                        • N
                                          Neil Schipper @Web Master
                                          last edited by Mar 4, 2022, 2:17 PM

                                          @web-master Does PM mean the chat? Sure go ahead, but I’m not sure if it supports file transfer. Another option is to put file on a hosting site so we can all access it.

                                          An easy thing to do is to gather up at least a few screens worth of “interesting” data (stressful/challenging to the algorithm) and tossing it into a “literal text” box as you’ve already learned to do (I’m not sure of upper size limit). (Here’s a trick: to defeat inappropriate colorization, add txt immediately after the opening line’s triple-backquotes).

                                          But over and above all of this, do notice that Alan’s post above provides a solution that is everything you dreamed of. You’ll need to install the Pythonscript plugin and learn a bit about making / saving / running a script (I’ve seen step-by-step recipes; I expect someone will link you to a reliable up-to-date one).

                                          Have fun.

                                          1 Reply Last reply Reply Quote 0
                                          19 out of 44
                                          • First post
                                            19/44
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors