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 qqqqqqqqqqqqAnd 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 qqqqqqqqqqqqThis 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 -
This will get you most of the way there:
- use
Ctrl+Hometo make sure you are at the start of the file - search/replace
- FIND =
(?-s).+\R.+\R.+(\R|\Z)\K - REPLACE =
[word]or[word]\r\n - disable ☐ wrap around
- mode = regular expression
Ctrl+Homethen 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. - use
-
@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 expressionSomething 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. :-) -
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 expressionThank 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$0Group
\1contains the current EOL char(s) of the fileBest Regards,
guy038
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