Find a line and copy the next 10 lines
-
I have a group of files that I need to find a common entry then copy the next 10 lines every time the entry is present. Is there a way to do this with Notepad++ ?
-
Check out the LineFilter2 plugin (it is available in the Plugin Manager). You can search and copy lines before/after the result.
-
Thanks for the reply that works only for the one file that is in focus. I have 30 log files ( or More) that I need find this entry in and the following 10 lines and copy to a log.
-
I needed the “lines after a match” capability today and was a bit shocked to find that LineFilter2 did not seem to be working correctly (it created a “New 1” file but it was empty, even for a very simple search).
I finally narrowed it down to the fact that the file I was searching was in Notepad++'s secondary editing view (it got there by right-clicking the tab for the file and choosing “Move to Other View”. If I moved the file back to the main view, LineFilter2 then processed the file correctly (it found the matches I expected).
-
Adding to what I just posted: I noticed the results of a LineFilter2 search initiated with a file in the secondary view sometimes end up in one of the active files in the main view, definitely not desired! I’d be curious to know if any other fans of the LineFilter2 plugin notice the same behavior.
-
@Jeff-Wiezorek I encourage anyone who programs or routinely works with log files and such learn some scripting language like AWK, PERL, Python, etc.
There will be many, many times in your career when you will be in a position to knock out some task like the one you mentioned above in a couple of minutes with one of these tools. Yes, you will have to invest some time up front to get proficient enough in your chosen language, but in the long run you will be far better off.
And don’t ignore the regular expression features of your chosen language! They will not only make your scripts more powerful, but the syntax will likely be very close to your editor’s regular expression syntax too, making you more efficient in the editor.
-
Hello Jeff and All,
As Jim, said in his post : with a scripting language, no file can resist :-)) However, as usual, the N++ regex engine should be enough, to get what you want :-))
A simple example : let’s suppose that you are looking for the litteral string 12345, anywhere, on a line.
-
First, to match all the contents of that specific line, as well as its End of Line characters, just use the regex :
^.*12345.*\R
-
Secondly, to select :
-
All the contents of that line and the next 10 lines, use the regex :
^.*12345.*\R(.*\R){10}
-
All the contents of that line and the previous 10 lines, use the regex :
(.*\R){10}^.*12345.*\R
-
All the contents of that line and the 5 lines, before and after this line, use the regex :
(.*\R){5}^.*12345.*\R(.*\R){5}
-
Note : For the third example, an other syntax, that uses a subroutine call, to the group 1,
(?1)
, is possible :(.*\R){5}^.*12345.*\R(?1){5}
That’s all !!
Best Regards,
guy038
-
-
Thanks Jim and All I am getting better with regular and extended expressions and they are a huge help. I found help for a UNIX guy and learned a little more as the files were all Gziped tar files and he was able to search them without opening them all and greped the text out that I needed. Keep learning!
-
@guy038 Hi i have a requirement where i am looking for string starting with
insert_job:AB
I want to bookmark all multiple occurances. The whole line containing insert_job:AB and 2 lines up and 10 lines down to this line.
Then i want to delete the unbookmarked lines and just save the ab previous output.
-
First off @Ankur-Sharma I will say that you should have created this as a new thread, not added to a thread started by someone else and which is quite old (essentially answered).
Now I will get on with a possible solution. I don’t know if you have tried the bookmark feature, if you did you likely found it would only bookmark 1 line in the group you want.
My idea is that you use the Replace function to add something unique to the start of the lines you want. Then you could remove all those lines which do NOT have this unique string at the start. I’ve used "X-X " but you could select something that suits your file (should not be anything that already exists in the file, possibly 2-3 characters long). These are regex and require the “regular expression” search mode selected and the wrap-around ticked.
Find what:
^(?-s)(.+\R)(.+\R)(insert_job:AB.*\R)(.+\R)(.+\R)(.+\R)(.+\R)(.+\R)(.+\R)(.+\R)(.+\R)(.+\R)(.+\R)
Replace with:X-X $1X-X $2X-X $3X-X $4X-X $5X-X $6X-X $7X-X $8X-X $9X-X $10X-X $11X-X $12X-X $13
Once this step is completed you would then run the following regex to remove all unwanted lines.
Find what:
(?-s)^[^X](?!-X )
Replace with:empty field here
<— nothing in this fieldThen you would finally run this regex to remove the characters we added in the first step.
Find what:
X-X
Replace with:empty field here
<— nothing in this fieldI hope this helps. There MUST be at least 10 lines after the last “insert_job:AB” line for the last occurance to match. If only 10 lines, then add an additional carriage return (enter key) to the last line.
Terry
-
Hello @ankur-sharma, @terry-r and All,
I’m thinking about a very easy solution, with a regex S/R, which does… all the job :-))
So, let’s imagine the initial text, below :
This a some dummy text to fill up the zone line 1 line 2 insert_job:AB line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 This a some dummy text to fill up the zone line 1 line 2 insert_job:AB line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 This a some dummy text to fill up the zone
Now :
-
Open the Replace dialog (
Ctrl + H
) -
Select the
Regular expression
search mode -
Tick the
Wrap around
option
SEARCH
(?s).*?(?-s)((?:^.*\R){2}insert_job:AB\R(?:^.*\R){10})|(?s).+
REPLACE
?1\1\r\n
- Clic on the
Replace All
button
Et voilà !
You should get, as below, the two expected areas of text, separated with a line-break ;-))
line 1 line 2 insert_job:AB line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 1 line 2 insert_job:AB line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10
Notes :
-
The main part of this regex is
(?:^.*\R){2}insert_job:AB\R(?:^.*\R){10}
, which matches :-
2
complete lines with their line-breaks, in a non-capturing group. So,(?:^.*\R){2}
-
The complete line insert_job:AB, with its like-break. So,
insert_job:AB\R
-
10
complete lines with their line-breaks, in a non-capturing group. So,(?:^.*\R){10}
-
-
As this main part is embedded in parentheses, it’s stored as group
1
, for further use , in replacement -
The part
(?s).*?
, at beginning, matches all the multi-lines stuff which precedes the main part -
When no more main part can be found, the regex engine tries the second regex
(?s).+
, placed after the alternation symbol (|
) -
This second regex matches all the remaining characters, after the last block of lines to keep till the very end of the file
Note that, if the string insert_job:AB is part of a line, you must change the part
insert_job:AB\R
with.*insert_job:AB.*\R
Remark : The method consists to use the following generic regex :
SEARCH
(?s).*?(
Your regex to match)|(?s).*
Refer to this post, for further explications :
https://notepad-plus-plus.org/community/topic/12710/marked-text-manipulation/8
Best Regards,
guy038
-