Community
    • Login

    add text to even lines

    Scheduled Pinned Locked Moved General Discussion
    10 Posts 5 Posters 1.7k 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.
    • cisco779kC
      cisco779k
      last edited by

      hi everybody, i have a long txt and i need to add some text only to even lines. how to?

      example:
      #summer
      0000-gold
      #summer
      0001-gold
      #summer
      0002-gold

      i need to have:
      #summer
      text/0000-gold
      #summer
      text/0001-gold
      #summer
      text/0002-gold

      Lycan ThropeL 1 Reply Last reply Reply Quote 0
      • Lycan ThropeL
        Lycan Thrope @cisco779k
        last edited by Lycan Thrope

        Find: (#summer\n)(.+)
        Replace: $1text/$2

        Alan KilbornA cisco779kC Terry RT 3 Replies Last reply Reply Quote 1
        • Alan KilbornA
          Alan Kilborn @Lycan Thrope
          last edited by

          @lycan-thrope

          Oooh, that’s not great. :-(

          Why?

          • summer was not in the requirements (much better would have been a requirement – of the solution – to move the caret to top of file before running, and thus the solution could take care of the “even lines” requirement)

          • \n is not good – if this file has windows line-endings, it isn’t going to work – use \R instead

          • there’s no specification to use regular expression search mode

          • there’s no specification to make sure that . matches newline is unticked (and no usage of a leading (?-s) on the find expression)

          1 Reply Last reply Reply Quote 3
          • cisco779kC
            cisco779k @Lycan Thrope
            last edited by

            @lycan-thrope said in add text to even lines:

            Find: (#summer\n)(.+)
            Replace: $1text/$2

            yes, this command does not work

            11.jpg

            1 Reply Last reply Reply Quote 0
            • Terry RT
              Terry R @Lycan Thrope
              last edited by Terry R

              @lycan-thrope

              I would change the \n for \R as we don’t know the format of the text file in terms of CR (carriage returns) and/or LF (line feeds) for line endings. This covers both bases.

              But; as in most cases; I suspect the example provided isn’t actually a good representation.

              When using a regular expression and selecting “Replace All” AND “Wrap around” the expression WILL start at line 1. So a regex that captures 2 lines will do so in the same manner requested. This was explained in a series of posts here provides a link to those.

              For example (?-s)^(.+\R)(.+\R?) will capture line 1 & 2. Then the next iteration will capture 3 & 4 and so on. The \R? is so the last line which will not have a CR and/or LF will still be captured.

              There is another method of determining the even numbered lines which involves some steps including adding numbers to the start of each line, as this would require placing the caret in the first position of the file we guarantee correct line placement. Each number would be repeated, so it shows twice, like 1 1 2 2 3 3 4 4… . Then a regex would remove the first one of the pair. The next regex could then identify the added number which references the “even” numbered line and use that as a key to edit the correct line. Lastly the added numbers could be removed. This is a much more involved process and I think unneeded as the “Replace All” and “Wrap around” option will work as intended.

              Terry

              PS my example regex assumes data on each line

              cisco779kC 1 Reply Last reply Reply Quote 2
              • cisco779kC
                cisco779k @Terry R
                last edited by

                thank you to all for replies

                anyway, thank to suggestion by Alan Kilborn, the string by Lycan Thrope work fine with simply replace:

                find what: (#summer\R)(.+)
                replace: $1text/$2

                cisco779kC 1 Reply Last reply Reply Quote 2
                • cisco779kC
                  cisco779k @cisco779k
                  last edited by

                  another question: from my long txt file, i have found some different value. sometime i have:

                  #summer:4,
                  0000-gold
                  #summer:4,
                  0001-gold
                  #summer:5.625,
                  0002-gold
                  #summer:4,
                  0003-gold
                  #summer:7.2,
                  0004-gold
                  

                  and i need to have:

                  #summer:4,
                  text/0000-gold
                  #summer:4,
                  text/0001-gold
                  #summer:5.625,
                  text/0002-gold
                  #summer:4,
                  text/0003-gold
                  #summer:7.2,
                  text/0004-gold

                  only the first part are the same #summer:
                  so, i have try with:

                  (#summer:\R)(.+)
                  replace: $1text/$2

                  but this command does not work. how to?

                  cisco779kC 1 Reply Last reply Reply Quote 0
                  • cisco779kC
                    cisco779k @cisco779k
                    last edited by

                    if this question it’s hard to get, another solution could be to look on my txt all the values that do not correspond to #summer:4, (as in the case of #summer:5.625,)

                    this would be enough for me

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

                      Hello, @cisco779k, @lycan-thrope, @alan-kilborn, @terry-r and All,

                      The correct regex, in this case, is :

                      SEARCH (?-s)^(#summer:.*\R)(.+)

                      REPLACE: $1text/$2


                      This search syntax will work, either, with lines #summer:.... and lines #summer !

                      Best Regards,

                      guy038

                      cisco779kC 1 Reply Last reply Reply Quote 2
                      • cisco779kC
                        cisco779k @guy038
                        last edited by

                        guy, thank you so much for your string. it work like a charm

                        all the best

                        1 Reply Last reply Reply Quote 1
                        • GabrielPG GabrielP referenced this topic on
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors