Community
    • Login

    Appropriate command to change lines using regular expressions

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 4 Posters 489 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.
    • J
      Jack
      last edited by

      Hello everyone, I would like to ask for your help in knowing the appropriate command to change the following lines using regular expressions.
      Initial data:

      00021  |03/01/2022|RECIBOS M.N.   |0661 |
       J4437            0011 0194
       J4502            0011 0194
       M077             0011 0182
       M111             0011 0355
      00024  |03/01/2022|RECIBOS M.N.   |0661 |
       7260             0011 0194
       7378             0011 0486
      00027  |03/01/2022|RECIBOS M.N.   |0661 |
       A1109            0011 0127
       A174             0011 0486
       A231             0011 0660
      00050  |03/01/2022|RECIBOS M.N.   |0661 |
      00053  |03/01/2022|RECIBOS M.N.   |0661 |
       A174             0011 0486
       A231             0011 0660 
      

      Expected data:

      00021  |03/01/2022|RECIBOS M.N.   |0661 |
      00021  |03/01/2022|RECIBOS M.N.   |0661 | J4437            0011 0194
      00021  |03/01/2022|RECIBOS M.N.   |0661 | J4502            0011 0194
      00021  |03/01/2022|RECIBOS M.N.   |0661 | M077             0011 0182
      00021  |03/01/2022|RECIBOS M.N.   |0661 | M111             0011 0355
      00024  |03/01/2022|RECIBOS M.N.   |0661 |
      00024  |03/01/2022|RECIBOS M.N.   |0661 | 7260             0011 0194
      00024  |03/01/2022|RECIBOS M.N.   |0661 | 7378             0011 0486
      00027  |03/01/2022|RECIBOS M.N.   |0661 |
      00027  |03/01/2022|RECIBOS M.N.   |0661 | A1109            0011 0127
      00027  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
      00027  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660
      00050  |03/01/2022|RECIBOS M.N.   |0661 |
      00053  |03/01/2022|RECIBOS M.N.   |0661 |
      00053  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
      00053  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660 
      

      Thank you very much for your help in advance.

      Terry RT 1 Reply Last reply Reply Quote 1
      • Terry RT
        Terry R @Jack
        last edited by

        @Jack
        This isn’t exactly the solution, but your request seems very similar to one made very recently. If you’d like to read this thread you may well find the method of doing what you want.

        Terry

        1 Reply Last reply Reply Quote 2
        • guy038G
          guy038
          last edited by guy038

          Hello, @jack, @terry-r and All,

          Terry, I think that the post, that you provided, may be too complicated for noob people to regexes !

          So, here a more easy solution for the @Jack’s specific problem !

          Jack, read this post thoroughly before doing any manipulation !


          Starting with your INPUT text, below :

          00021  |03/01/2022|RECIBOS M.N.   |0661 |
           J4437            0011 0194
           J4502            0011 0194
           M077             0011 0182
           M111             0011 0355
          00024  |03/01/2022|RECIBOS M.N.   |0661 |
           7260             0011 0194
           7378             0011 0486
          00027  |03/01/2022|RECIBOS M.N.   |0661 |
           A1109            0011 0127
           A174             0011 0486
           A231             0011 0660
          00050  |03/01/2022|RECIBOS M.N.   |0661 |
          00053  |03/01/2022|RECIBOS M.N.   |0661 |
           A174             0011 0486
           A231             0011 0660 
          
          • To begin with, add a final line-break \r\n at the very end of this INPUT file

          Then, with this first regex S/R, we’re going to invert each ligne beginning with a digit with the next existing group of lines beginning with a space character :

          • SEARCH (?x-s) ^ ( \d .+ \R ) ( (?: \x20 .+ \R )+ )

          • REPLACE \2\1

          We get this temporary OUTPUT text :

           J4437            0011 0194
           J4502            0011 0194
           M077             0011 0182
           M111             0011 0355
          00021  |03/01/2022|RECIBOS M.N.   |0661 |
           7260             0011 0194
           7378             0011 0486
          00024  |03/01/2022|RECIBOS M.N.   |0661 |
           A1109            0011 0127
           A174             0011 0486
           A231             0011 0660
          00027  |03/01/2022|RECIBOS M.N.   |0661 |
          00050  |03/01/2022|RECIBOS M.N.   |0661 |
           A174             0011 0486
           A231             0011 0660 
          00053  |03/01/2022|RECIBOS M.N.   |0661 |
          

          Now, with this second regex S/R, we add the corresponding header, in front of each line beginning with a space char :

          • SEARCH (?x-s) ^ \x20 .+ \R (?= (?: \x20 .+ \R )* ( \d+ .+ ) )

          • REPLACE \1$0

          And our temporary OUTPUT text becomes :

          00021  |03/01/2022|RECIBOS M.N.   |0661 | J4437            0011 0194
          00021  |03/01/2022|RECIBOS M.N.   |0661 | J4502            0011 0194
          00021  |03/01/2022|RECIBOS M.N.   |0661 | M077             0011 0182
          00021  |03/01/2022|RECIBOS M.N.   |0661 | M111             0011 0355
          00021  |03/01/2022|RECIBOS M.N.   |0661 |
          00024  |03/01/2022|RECIBOS M.N.   |0661 | 7260             0011 0194
          00024  |03/01/2022|RECIBOS M.N.   |0661 | 7378             0011 0486
          00024  |03/01/2022|RECIBOS M.N.   |0661 |
          00027  |03/01/2022|RECIBOS M.N.   |0661 | A1109            0011 0127
          00027  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
          00027  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660
          00027  |03/01/2022|RECIBOS M.N.   |0661 |
          00050  |03/01/2022|RECIBOS M.N.   |0661 |
          00053  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
          00053  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660 
          00053  |03/01/2022|RECIBOS M.N.   |0661 |
          

          Finally, with this third regex S/R, we invert, again, an existing group of lines not ending with the pipe | character and the next line ending with the pipe | character :

          • SEARCH (?x-s) ^ ( (?: .+ [^|\r\n] \R )+ ) ( .+ \| \R )

          • REPLACE \2\1

          And here is your expected OUTPUT text :

          00021  |03/01/2022|RECIBOS M.N.   |0661 |
          00021  |03/01/2022|RECIBOS M.N.   |0661 | J4437            0011 0194
          00021  |03/01/2022|RECIBOS M.N.   |0661 | J4502            0011 0194
          00021  |03/01/2022|RECIBOS M.N.   |0661 | M077             0011 0182
          00021  |03/01/2022|RECIBOS M.N.   |0661 | M111             0011 0355
          00024  |03/01/2022|RECIBOS M.N.   |0661 |
          00024  |03/01/2022|RECIBOS M.N.   |0661 | 7260             0011 0194
          00024  |03/01/2022|RECIBOS M.N.   |0661 | 7378             0011 0486
          00027  |03/01/2022|RECIBOS M.N.   |0661 |
          00027  |03/01/2022|RECIBOS M.N.   |0661 | A1109            0011 0127
          00027  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
          00027  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660
          00050  |03/01/2022|RECIBOS M.N.   |0661 |
          00053  |03/01/2022|RECIBOS M.N.   |0661 |
          00053  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
          00053  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660 
          

          For all these searches/replacements :

          • Move at the very beginning of your file

          • Open the Replace dialog ( Ctrl + H )

          • Uncheck all box options

          • Insert the corresponding search and replace regexes in the Find what : and Replace with : fields

          • Select the Regular expression search mode

          • Click once on the Replace All button ( or several times on the Replace button )


          If you need some explanations on these regexes, just tell me !

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 3
          • guy038G
            guy038
            last edited by guy038

            Hi, @jack, @terry-r and All,

            Later, I realized that my first regex S/R, provided in my previous post, could be changed into this one:

            • SEARCH (?x-s) ^ ( .+ \| \R ) ( (?: .+ [^|\r\n] \R )+ )

            • REPLACE \2\1

            So, with this first regex S/R and the initial INPUT text :

            We’re going to invert each line ending with a pipe | character and the next existing group of lines not ending with a pipe | character


            In this way, just note that this NEW first regex S/R is the true opposite of the third regex S/R :-;

            BR

            guy038

            J 1 Reply Last reply Reply Quote 1
            • J
              Jack @guy038
              last edited by

              Hi! @guy038 Thank you very much for your support. However, I have an issue, as I was reviewing all the cases, I found new scenarios that do not fit the previous ones. I would appreciate it if you could help me with this case.

              Initial data:

              00021  |03/01/2022|RECIBOS M.N.   |0661 |
               M077             0011 0182
               M111             0011 0355
              00024  |03/01/2022|RECIBOS M.N.   |0661 |
               7260             0011 0194
               7378             0011 0486
              00027  |03/01/2022|RECIBOS M.N.   |0661 |
               A1109            0011 0127
               A174             0011 0486
               A231             0011 0660
              00050  |03/01/2022|RECIBOS M.N.   |0661 |
              00053  |03/01/2022|RECIBOS M.N.   |0661 |
               A174             0011 0486
               A231             0011 0660 
              B9295  |02/01/2023|RECIBOS M.N.   |0451 |  
               00315178         0011 0057
               00547680         0011 0814
              00059  |02/01/2023|SEGUROS M.E.   |0451 |
               E203172          0011 0181
               E6725755962      0011 0174
               E6726796044      0011 0204
               E6726902910      0011 0372
              00175  |02/01/2023|SEGUROS M.N.   |0451 |
               S00000050000989  0011 0274
              00175  |02/01/2023|SEGUROS M.E.   |0451 |
              00175  |02/01/2023|SEGUROS M.E.   |0451 |
               000000000204011  0011 0333
               000000000297155  0011 0223
              
              

              Expected data:

              00021  |03/01/2022|RECIBOS M.N.   |0661 |
              00021  |03/01/2022|RECIBOS M.N.   |0661 | M077             0011 0182
              00021  |03/01/2022|RECIBOS M.N.   |0661 | M111             0011 0355
              00024  |03/01/2022|RECIBOS M.N.   |0661 |
              00024  |03/01/2022|RECIBOS M.N.   |0661 | 7260             0011 0194
              00024  |03/01/2022|RECIBOS M.N.   |0661 | 7378             0011 0486
              00027  |03/01/2022|RECIBOS M.N.   |0661 |
              00027  |03/01/2022|RECIBOS M.N.   |0661 | A1109            0011 0127
              00027  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
              00027  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660
              00050  |03/01/2022|RECIBOS M.N.   |0661 |
              00053  |03/01/2022|RECIBOS M.N.   |0661 |
              00053  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
              00053  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660 
              B9295  |02/01/2023|RECIBOS M.N.   |0451 |
              B9295  |02/01/2023|RECIBOS M.N.   |0451 | 00315178         0011 0057
              B9295  |02/01/2023|RECIBOS M.N.   |0451 | 00547680         0011 0814
              00059  |02/01/2023|SEGUROS M.E.   |0451 |
              00059  |02/01/2023|SEGUROS M.E.   |0451 | E203172          0011 0181
              00059  |02/01/2023|SEGUROS M.E.   |0451 | E6725755962      0011 0174
              00059  |02/01/2023|SEGUROS M.E.   |0451 | E6726796044      0011 0204
              00059  |02/01/2023|SEGUROS M.E.   |0451 | E6726902910      0011 0372
              00175  |02/01/2023|SEGUROS M.N.   |0451 |
              00175  |02/01/2023|SEGUROS M.N.   |0451 | S00000050000989  0011 0274
              00175  |02/01/2023|SEGUROS M.E.   |0451 |
              00175  |02/01/2023|SEGUROS M.E.   |0451 |
              00175  |02/01/2023|SEGUROS M.E.   |0451 | 000000000204011  0011 0333
              00175  |02/01/2023|SEGUROS M.E.   |0451 | 000000000297155  0011 0223
              
              
              Mark OlsonM 1 Reply Last reply Reply Quote 0
              • Mark OlsonM
                Mark Olson @Jack
                last edited by

                @Jack
                Not an answer, but a hint: guy038 mentioned in various places that he wanted to match lines beginning with a digit. The regex he used to match a digit was \d. Read a little bit about regular expressions and see if you can find how to match something besides a digit.

                1 Reply Last reply Reply Quote 2
                • guy038G
                  guy038
                  last edited by guy038

                  Hi, @jack, @terry-r, @mark-olson and All,

                  OK : I understood what happened. You have some trailing spaces at the end of some lines of your INPUT text !

                  Moreover, some header lines do not begin with digits but with word characters, so we need \w+ ( instead of \d+ )

                  So, starting with your INPUT text :

                  00021  |03/01/2022|RECIBOS M.N.   |0661 |
                   M077             0011 0182
                   M111             0011 0355
                  00024  |03/01/2022|RECIBOS M.N.   |0661 |
                   7260             0011 0194
                   7378             0011 0486
                  00027  |03/01/2022|RECIBOS M.N.   |0661 |
                   A1109            0011 0127
                   A174             0011 0486
                   A231             0011 0660
                  00050  |03/01/2022|RECIBOS M.N.   |0661 |
                  00053  |03/01/2022|RECIBOS M.N.   |0661 |
                   A174             0011 0486
                   A231             0011 0660 
                  B9295  |02/01/2023|RECIBOS M.N.   |0451 |  
                   00315178         0011 0057
                   00547680         0011 0814
                  00059  |02/01/2023|SEGUROS M.E.   |0451 |
                   E203172          0011 0181
                   E6725755962      0011 0174
                   E6726796044      0011 0204
                   E6726902910      0011 0372
                  00175  |02/01/2023|SEGUROS M.N.   |0451 |
                   S00000050000989  0011 0274
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                   000000000204011  0011 0333
                   000000000297155  0011 0223
                  

                  Before the main regexes S/R to perform, first run this simple S/R which deletes any trailing horizontal blank character :

                  SEARCH \h+$

                  REPLACE Leave EMPTY

                  You’ll get this temporary OUTPUT text :

                  00021  |03/01/2022|RECIBOS M.N.   |0661 |
                   M077             0011 0182
                   M111             0011 0355
                  00024  |03/01/2022|RECIBOS M.N.   |0661 |
                   7260             0011 0194
                   7378             0011 0486
                  00027  |03/01/2022|RECIBOS M.N.   |0661 |
                   A1109            0011 0127
                   A174             0011 0486
                   A231             0011 0660
                  00050  |03/01/2022|RECIBOS M.N.   |0661 |
                  00053  |03/01/2022|RECIBOS M.N.   |0661 |
                   A174             0011 0486
                   A231             0011 0660
                  B9295  |02/01/2023|RECIBOS M.N.   |0451 |
                   00315178         0011 0057
                   00547680         0011 0814
                  00059  |02/01/2023|SEGUROS M.E.   |0451 |
                   E203172          0011 0181
                   E6725755962      0011 0174
                   E6726796044      0011 0204
                   E6726902910      0011 0372
                  00175  |02/01/2023|SEGUROS M.N.   |0451 |
                   S00000050000989  0011 0274
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                   000000000204011  0011 0333
                   000000000297155  0011 0223
                  

                  Then, with this first regex S/R :

                  SEARCH (?x-s) ^ ( .+ \| \R ) ( (?: \x20 .+ \R )+ )

                  REPLACE \2\1

                  The temporary OUTPUT text becomes :

                   M077             0011 0182
                   M111             0011 0355
                  00021  |03/01/2022|RECIBOS M.N.   |0661 |
                   7260             0011 0194
                   7378             0011 0486
                  00024  |03/01/2022|RECIBOS M.N.   |0661 |
                   A1109            0011 0127
                   A174             0011 0486
                   A231             0011 0660
                  00027  |03/01/2022|RECIBOS M.N.   |0661 |
                  00050  |03/01/2022|RECIBOS M.N.   |0661 |
                   A174             0011 0486
                   A231             0011 0660
                  00053  |03/01/2022|RECIBOS M.N.   |0661 |
                   00315178         0011 0057
                   00547680         0011 0814
                  B9295  |02/01/2023|RECIBOS M.N.   |0451 |
                   E203172          0011 0181
                   E6725755962      0011 0174
                   E6726796044      0011 0204
                   E6726902910      0011 0372
                  00059  |02/01/2023|SEGUROS M.E.   |0451 |
                   S00000050000989  0011 0274
                  00175  |02/01/2023|SEGUROS M.N.   |0451 |
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                   000000000204011  0011 0333
                   000000000297155  0011 0223
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                  

                  Now, with the second regex S/R :

                  SEARCH (?x-s) ^ \x20 .+ \R (?= (?: \x20 .+ \R )* ( \w+ .+ ) )

                  REPLACE \1$0

                  we get the temporary OUTPUT text :

                  00021  |03/01/2022|RECIBOS M.N.   |0661 | M077             0011 0182
                  00021  |03/01/2022|RECIBOS M.N.   |0661 | M111             0011 0355
                  00021  |03/01/2022|RECIBOS M.N.   |0661 |
                  00024  |03/01/2022|RECIBOS M.N.   |0661 | 7260             0011 0194
                  00024  |03/01/2022|RECIBOS M.N.   |0661 | 7378             0011 0486
                  00024  |03/01/2022|RECIBOS M.N.   |0661 |
                  00027  |03/01/2022|RECIBOS M.N.   |0661 | A1109            0011 0127
                  00027  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
                  00027  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660
                  00027  |03/01/2022|RECIBOS M.N.   |0661 |
                  00050  |03/01/2022|RECIBOS M.N.   |0661 |
                  00053  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
                  00053  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660
                  00053  |03/01/2022|RECIBOS M.N.   |0661 |
                  B9295  |02/01/2023|RECIBOS M.N.   |0451 | 00315178         0011 0057
                  B9295  |02/01/2023|RECIBOS M.N.   |0451 | 00547680         0011 0814
                  B9295  |02/01/2023|RECIBOS M.N.   |0451 |
                  00059  |02/01/2023|SEGUROS M.E.   |0451 | E203172          0011 0181
                  00059  |02/01/2023|SEGUROS M.E.   |0451 | E6725755962      0011 0174
                  00059  |02/01/2023|SEGUROS M.E.   |0451 | E6726796044      0011 0204
                  00059  |02/01/2023|SEGUROS M.E.   |0451 | E6726902910      0011 0372
                  00059  |02/01/2023|SEGUROS M.E.   |0451 |
                  00175  |02/01/2023|SEGUROS M.N.   |0451 | S00000050000989  0011 0274
                  00175  |02/01/2023|SEGUROS M.N.   |0451 |
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                  00175  |02/01/2023|SEGUROS M.E.   |0451 | 000000000204011  0011 0333
                  00175  |02/01/2023|SEGUROS M.E.   |0451 | 000000000297155  0011 0223
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                  

                  Finally, with this third regex S/R :

                  SEARCH (?x-s) ^ ( (?: .+ [^|\r\n] \R )+ ) ( .+ \| \R )

                  REPLACE \2\1

                  Here is your expected OUTPUT text :

                  00021  |03/01/2022|RECIBOS M.N.   |0661 |
                  00021  |03/01/2022|RECIBOS M.N.   |0661 | M077             0011 0182
                  00021  |03/01/2022|RECIBOS M.N.   |0661 | M111             0011 0355
                  00024  |03/01/2022|RECIBOS M.N.   |0661 |
                  00024  |03/01/2022|RECIBOS M.N.   |0661 | 7260             0011 0194
                  00024  |03/01/2022|RECIBOS M.N.   |0661 | 7378             0011 0486
                  00027  |03/01/2022|RECIBOS M.N.   |0661 |
                  00027  |03/01/2022|RECIBOS M.N.   |0661 | A1109            0011 0127
                  00027  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
                  00027  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660
                  00050  |03/01/2022|RECIBOS M.N.   |0661 |
                  00053  |03/01/2022|RECIBOS M.N.   |0661 |
                  00053  |03/01/2022|RECIBOS M.N.   |0661 | A174             0011 0486
                  00053  |03/01/2022|RECIBOS M.N.   |0661 | A231             0011 0660
                  B9295  |02/01/2023|RECIBOS M.N.   |0451 |
                  B9295  |02/01/2023|RECIBOS M.N.   |0451 | 00315178         0011 0057
                  B9295  |02/01/2023|RECIBOS M.N.   |0451 | 00547680         0011 0814
                  00059  |02/01/2023|SEGUROS M.E.   |0451 |
                  00059  |02/01/2023|SEGUROS M.E.   |0451 | E203172          0011 0181
                  00059  |02/01/2023|SEGUROS M.E.   |0451 | E6725755962      0011 0174
                  00059  |02/01/2023|SEGUROS M.E.   |0451 | E6726796044      0011 0204
                  00059  |02/01/2023|SEGUROS M.E.   |0451 | E6726902910      0011 0372
                  00175  |02/01/2023|SEGUROS M.N.   |0451 |
                  00175  |02/01/2023|SEGUROS M.N.   |0451 | S00000050000989  0011 0274
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                  00175  |02/01/2023|SEGUROS M.E.   |0451 |
                  00175  |02/01/2023|SEGUROS M.E.   |0451 | 000000000204011  0011 0333
                  00175  |02/01/2023|SEGUROS M.E.   |0451 | 000000000297155  0011 0223
                  

                  Best Regards,

                  guy038

                  1 Reply Last reply Reply Quote 0
                  • guy038G
                    guy038
                    last edited by guy038

                    Hi, @terry-r,

                    I’ve just had a look to the picture of your profile … Very nice ;-))

                    BR

                    guy038

                    J 1 Reply Last reply Reply Quote 2
                    • J
                      Jack @guy038
                      last edited by

                      @guy038 I didn’t have the chance to thank you very much for your support! It worked very well!

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