• Login
Community
  • Login

adding word between any 3 lines

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
7 Posts 4 Posters 1.3k 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.
  • H
    hasan123
    last edited by Jan 6, 2020, 4:02 PM

    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
    • A
      Alan Kilborn
      last edited by Jan 6, 2020, 5:48 PM

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

      P H 2 Replies Last reply Jan 6, 2020, 5:52 PM Reply Quote 2
      • P
        PeterJones
        last edited by Jan 6, 2020, 5:50 PM

        @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
        • P
          PeterJones @Alan Kilborn
          last edited by Jan 6, 2020, 5:52 PM

          @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. :-)

          A 1 Reply Last reply Jan 6, 2020, 6:00 PM Reply Quote 2
          • A
            Alan Kilborn @PeterJones
            last edited by Jan 6, 2020, 6:00 PM

            @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
            • H
              hasan123 @Alan Kilborn
              last edited by Jan 6, 2020, 6:36 PM

              @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
              • G
                guy038
                last edited by guy038 Jan 7, 2020, 10:58 AM Jan 7, 2020, 12:03 AM

                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
                4 out of 7
                • First post
                  4/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors