adding word between any 3 lines



  • 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
    


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



  • @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.



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



  • @PeterJones

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



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



  • 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


Log in to reply