• Login
Community
  • Login

Adding text with variable on several files

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
13 Posts 5 Posters 1.5k 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.
  • V
    Vincent Perez
    last edited by May 3, 2021, 7:03 PM

    HI, I’m quite green with python scripting, and I have found a problem which is well above my knowledge of notepad++ and python.

    I have several files with the structure:
    HEADER

    party = {
    name = “ALE_section_four”
    412354

    party = {
    name = “ALE_section_five”
    7655444556

    party = {
    name = “ALE_section_six”
    876533454

    The thing is I want to store the three characters just before _section (ALE in this case) and then copy the following text just after the HEADER,

    party = {
    name = “s%_section_one”
    62194678

    party = {
    name = “s%_section_two”
    112093

    party = {
    name = “s%_section_three”
    87321345

    replacing the s% for the stored variable (ALE in this case). so the final result would be something like this:

    HEADER

    party = {
    name = “ALE_section_one”
    62194678

    party = {
    name = “ALE_section_two”
    112093

    party = {
    name = “ALE_section_three”
    87321345

    party = {
    name = “ALE_section_four”
    412354

    party = {
    name = “ALE_section_five”
    7655444556

    party = {
    name = “ALE_section_six”
    876533454

    This is well beyond my current abilities, so i would accept any help on the issue, thank you very much.

    A V 2 Replies Last reply May 3, 2021, 8:32 PM Reply Quote 0
    • A
      Alan Kilborn @Vincent Perez
      last edited by May 3, 2021, 8:32 PM

      @Vincent-Perez

      I suppose something like this works:

      Set search mode to Regular expression

      find:

      (?-s)^HEADER(\R)\R(?=party = {\Rname = "(...)_section_four")
      

      replace:

      HEADER${1}${1}party = {${1}name = "${2}_section_one"${1}62194678${1}${1}party = {${1}name = "${2}_section_two"${1}112093${1}${1}party = {${1}name = "${2}_section_three"${1}87321345
      
      V 1 Reply Last reply May 3, 2021, 8:53 PM Reply Quote 1
      • V
        Vincent Perez @Alan Kilborn
        last edited by May 3, 2021, 8:53 PM

        @Alan-Kilborn Is not finding it

        79d31e63-fc0b-4cc7-b4f9-a133b06e42e7-imagen.png

        A 1 Reply Last reply May 3, 2021, 9:06 PM Reply Quote 0
        • A
          Alan Kilborn @Vincent Perez
          last edited by Alan Kilborn May 3, 2021, 9:06 PM May 3, 2021, 9:06 PM

          @Vincent-Perez

          You may need to adjust your double-quote characters in the “find” and “replace” expression I gave.

          If you look closely at your screenshot data, you’ll see that yours are “slanted” and I used “straight” in my expressions. They are not interchangeable – welcome to a unicode world.

          I did what I did because you pasted data into this site without preserving its format, so I made my “best guess” at what you had.

          Or, it could be something else wrong.
          But…I’d start there.

          1 Reply Last reply Reply Quote 1
          • V
            Vincent Perez @Vincent Perez
            last edited by May 3, 2021, 9:14 PM

            @Vincent-Perez said in Adding text with variable on several files:

            “ALE_section_one”

            You were right! thank you very much, you have saved me a lot of hours of boring copy paste :-)

            1 Reply Last reply Reply Quote 1
            • G
              guy038
              last edited by guy038 May 4, 2021, 12:59 AM May 4, 2021, 12:44 AM

              Hi, @vincent-perez, @alan-kilborn and All,

              If I suppose that your text contains, after the HEADER line, n blocks as below ( syntax xxx means any text ) :

              
              party = {
              name = "s%_section_xxx"
              xxxxx
              

              In turn, followed with m blocks as below :

              
              party = {
              name = "XXX_section_xxx"
              xxxxx
              

              And if m is greater than 0 ( i.e. if exists, at least, one block with a line name = "XXX_section_xxx" where the string XXX must replace the string s% )

              An other solution could be :

              SEARCH (?s-i)s%(?=.*?^name\x20=\x20"(\w+)_section)

              REPLACE \1


              Notes :

              • Unlike the @alan-kilborn’s solution, this S/R does not care about numbers, under the name = ••• line and about text after _section_, too. It just try to match any %s string and stores the first XXX string found, before _section, for use in replacement

              • Note that if NO block with a line name = "XXX_section_xxx" can be found in a whole section ( case m = 0 ), all strings s%, in lines name = "s%_section_xxx" of this section, would be changed as XXX, where XXX comes from the first name = "XXX_section_xxx" line of the next section

              Best Regards,

              guy038

              R 1 Reply Last reply May 5, 2021, 1:22 PM Reply Quote 0
              • R
                Ramanand Jhingade @guy038
                last edited by May 5, 2021, 1:22 PM

                @guy038 I have to add 9 lines of matter (text), all on new lines, one after the other in multiple files after this code/text: <button class=“button-top”><i class=“fa fa-chevron-up”></i></button> - how do I do it?

                P R 2 Replies Last reply May 5, 2021, 1:58 PM Reply Quote 0
                • P
                  PeterJones @Ramanand Jhingade
                  last edited by PeterJones May 5, 2021, 1:58 PM May 5, 2021, 1:58 PM

                  @Ramanand-Jhingade said in Adding text with variable on several files:

                  all on new lines, one after the other

                  Using \r\n in the REPLACE field indicates the CRLF newline. (If you’re editing a file with a linux LF newline, just use \n).

                  So if you wanted the replacement to be

                  blah
                  second line
                  third line
                  

                  then REPLACE would be blah\r\nsecond line\r\nthird line

                  1 Reply Last reply Reply Quote 1
                  • R
                    Ramanand Jhingade @Ramanand Jhingade
                    last edited by Ramanand Jhingade May 5, 2021, 2:04 PM May 5, 2021, 2:01 PM

                    @Ramanand-Jhingade The only solution I can think of now is to use the “Extended” mode of search, search for <button class=“button-top”><i class=“fa fa-chevron-up”></i></button> and replace it with <button class=“button-top”><i class=“fa fa-chevron-up”></i></button>\nxxxxxxxxx where xxxxxxxxx is the new line for each line, 9 times but is there a way to do it all together?
                    Apart from what @PeterJones typed above?

                    P 1 Reply Last reply May 5, 2021, 3:01 PM Reply Quote 0
                    • P
                      PeterJones @Ramanand Jhingade
                      last edited by PeterJones May 5, 2021, 3:02 PM May 5, 2021, 3:01 PM

                      @Ramanand-Jhingade ,

                      Sorry, yes, my reply was assuming Regular Expression mode, because that’s the mode that was being used in the conversation between @Vincent-Perez and @guy038 – I didn’t realize when I replied that you weren’t the original poster.

                      With 9 lines, you will just have to have nine instances of \r\nxxxxxxx in the replacement.

                      To avoid some duplication of data, I suggest you use the $0 replacement (see docs ), which will use the value matched from the FIND, rather than manually retyping that initial data that you want to keep:

                      • FIND = <button class="button-top"><i class="fa fa-chevron-up"></i></button>
                      • REPLACE = $0\r\nline 1\r\nline2\r\nline3\r\nline 4\r\nline 5\r\nline six\r\nline 7\r\nline eight\r\nline 9!
                      • SEARCH MODE = regular expression

                      (I continue to use \r\n in my examples, because chances are, since you’re editing files on using a windows application, the newlines are probably windows-style CRLF newlines)

                      R 1 Reply Last reply May 5, 2021, 3:18 PM Reply Quote 1
                      • R
                        Ramanand Jhingade @PeterJones
                        last edited by Ramanand Jhingade May 5, 2021, 3:19 PM May 5, 2021, 3:18 PM

                        @PeterJones Wonderful but do I need that exclamation mark at the end (in the REPLACE field)?

                        A 1 Reply Last reply May 5, 2021, 3:24 PM Reply Quote 0
                        • A
                          Alan Kilborn @Ramanand Jhingade
                          last edited by May 5, 2021, 3:24 PM

                          @Ramanand-Jhingade said in Adding text with variable on several files:

                          but do I need that exclamation mark at the end (in the REPLACE field)?

                          Only if you want it there.
                          Honestly, some experimentation will quickly provide the answer to that, rather than asking here and waiting the time it takes for a response.

                          R 1 Reply Last reply May 5, 2021, 3:57 PM Reply Quote 1
                          • R
                            Ramanand Jhingade @Alan Kilborn
                            last edited by May 5, 2021, 3:57 PM

                            @Alan-Kilborn Yes, you’re right. Thanks for the help @Alan-Kilborn @PeterJones and @guy038

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