Community
    • Login

    adding word between any 3 lines

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    7 Posts 4 Posters 2.0k Views 2 Watching
    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.
    • hasan123H Offline
      hasan123
      last edited by

      Hi, Happy New Year
      I have text file like this:

      aaaaaaaaaaaa
      bbbbbbbbbbbb
      cccccccccccc
      dddddddddddd
      eeeeeeeeeeee
      ffffffffffff
      gggggggggggg
      hhhhhhhhhhhh
      iiiiiiiiiiii
      jjjjjjjjjjjj
      kkkkkkkkkkkk
      llllllllllll
      mmmmmmmmmmmm
      nnnnnnnnnnnn
      oooooooooooo
      pppppppppppp
      qqqqqqqqqqqq
      

      And i want to add a word/code between any 3 lines,
      How can i do this:

      [word]aaaaaaaaaaaa
      bbbbbbbbbbbb
      cccccccccccc
      [word]dddddddddddd
      eeeeeeeeeeee
      ffffffffffff
      [word]gggggggggggg
      hhhhhhhhhhhh
      iiiiiiiiiiii
      [word]jjjjjjjjjjjj
      kkkkkkkkkkkk
      llllllllllll
      [word]mmmmmmmmmmmm
      nnnnnnnnnnnn
      oooooooooooo
      [word]pppppppppppp
      qqqqqqqqqqqq
      

      This one is also ok:

      [word]
      aaaaaaaaaaaa
      bbbbbbbbbbbb
      cccccccccccc
      [word]
      dddddddddddd
      eeeeeeeeeeee
      ffffffffffff
      [word]
      gggggggggggg
      hhhhhhhhhhhh
      iiiiiiiiiiii
      [word]
      jjjjjjjjjjjj
      kkkkkkkkkkkk
      llllllllllll
      [word]
      mmmmmmmmmmmm
      nnnnnnnnnnnn
      oooooooooooo
      [word]
      pppppppppppp
      qqqqqqqqqqqq
      
      1 Reply Last reply Reply Quote 0
      • Alan KilbornA Offline
        Alan Kilborn
        last edited by

        I would try searching for (?-s)^(?:.*\R){3} and replacing with [word]$0. Search mode = Regular expression

        PeterJonesP hasan123H 2 Replies Last reply Reply Quote 2
        • PeterJonesP Online
          PeterJones
          last edited by

          @hasan123,

          This will get you most of the way there:

          1. use Ctrl+Home to make sure you are at the start of the file
          2. search/replace
          • FIND = (?-s).+\R.+\R.+(\R|\Z)\K
          • REPLACE = [word] or [word]\r\n
          • disable ☐ wrap around
          • mode = regular expression
          1. Ctrl+Home then manually insert [word] at the start of the file.

          I tried for some alternation, using \A^|... or similar to get it to also replace at the start of the file, but five minutes of experimenting wasn’t enough to get it to work. Maybe @guy038 will chime in with an all-in-one, but for me, the curiosity of knowing how to get it in one regex doesn’t outweigh the additional time beyond 5min for exploring the intricacies of regex, when a few seconds of typing will add in the missing [word] at the beginning of the file.

          1 Reply Last reply Reply Quote 1
          • PeterJonesP Online
            PeterJones @Alan Kilborn
            last edited by

            @Alan-Kilborn said in adding word between any 3 lines:

            I would try searching for (?-s)^(?:.*\R){3} and replacing with [word]$0. Search mode = Regular expression

            Something similar was my first thought, but that doesn’t get the [word] before the p and q lines, which was in the original spec. I guess, like my solution, @hasan123 could manually do one [word] insertion. :-)

            Alan KilbornA 1 Reply Last reply Reply Quote 2
            • Alan KilbornA Offline
              Alan Kilborn @PeterJones
              last edited by

              @PeterJones

              Ha, didn’t even notice that. Well, I guess the OP is most of the way there.

              1 Reply Last reply Reply Quote 2
              • hasan123H Offline
                hasan123 @Alan Kilborn
                last edited by

                @Alan-Kilborn said in adding word between any 3 lines:

                I would try searching for (?-s)^(?:.*\R){3} and replacing with [word]$0. Search mode = Regular expression

                Thank you very much, it’s do the job for me :)

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

                  Hello, @Hasan123, @Alan-kilborn, @peterjones and All,

                  Here is my very similar version :

                  SEARCH (?-s)(?:^.+(?:(\R)|\Z)){1,3}

                  [word]\1$0

                  Group \1 contains the current EOL char(s) of the file

                  Best Regards,

                  guy038

                  1 Reply Last reply Reply Quote 3

                  Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                  Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                  With your input, this post could be even better 💗

                  Register Login
                  • First post
                    Last post
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors