Show (or keep) subsets of a file
-
I have a file that contains many many blocks of text. Each block starts with a line containing <start string> and ends with a line containing <end string>. I’m only interested in those blocks that have <target string> somewhere in the block; there are far fewer occurrences of <target string> than <start string> but still too many to process manually. What I’d like to be able to do is find each occurrence of <target string>and keep only those lines from the preceding <start string> to the next <end string>. I don’t really care whether everything else is hidden or deleted. Is there a way to do something like this? Thanks for any suggestions.
-
Hello, @mark-boonie and All,
Mark, I think there is a way to achieve what you want with regular expressions !
Could you provide one or two examples of these
<start string>......<end string>blocks ?When posting, try to first hit the
</>Code button to ensure that your text is copied literallySee you later,
Best Regards,
guy038
-
Hi @guy038. I’m changing it a bit for business reasons, but basically it would look like this:
*Block start 00000000013FC200 00200280 00010000 00000000 00000000 00000000013FC210 00000002 CC5CDDA0 00000000 00000000 00000000013FC220 00000000 00000000 01266100 01266100 00000000013FC230 00808000 013FC2B8 00000000 00000000 00000000013FC240 0003D000 03A8A1A0 03A8A670 03A8A710 00000000013FC250 00000000 0003DD88 013FD020 00000000 00000000013FC260 00000000 00000000 00000000 0C000002 00000000013FC270 11804017 03A8A718 0E000000 00800000 00000000013FC280 40000020 013FC280 00000000 00000000 00000000013FC290 00000000 00000000 00000000 00000000 00000000013FC2A0 00000000 0C000002 10800011 00000000 00000000013FC2B0 06000000 00800000 40000020 013FC2B8 00000000013FC2C0 00000000 00000000 00000000 00000000 00000000013FC320 00000000 01421800 00000000 00000000 00000000013FC330 00000000 00000000 00000000 00000000 00000000013FC3C0 00000000 00000000 00000000 00000571 00000000013FC3D0 00000000 00000000 00000000 00000000 00000000013FC3F0 00000000 00000000 00000000 00000000 *Block endThe first and last lines shown are the delimiter lines. The target string would vary, but obviously it’s another hex string. Thanks for any suggestions.
-
Hi, @mark-boonie,
Hum, I’m a bit upset with the example that you provided !
Indeed, I’ve already find out a regex solution, following exactly what you said in your initial post
So, I created this sample of text below :
bla bla <start string> blo blo <target string> <end string> bla bal blah blah <start string> <end string> <start string> <target string> blu blu <end string> bla bla <start string> bla bla blah blah <end string> bla bla <start string> <target string> <end string> bla blaNow :
-
Open the Replace dialog (
Ctrl + H) -
Uncheck any box option
-
Find
(?s)^<start string>((?!<start string>).)+?<target string>.+?^<end string>\R(*SKIP)(*F)|(?-s)^.*\R -
Replace
Leave EMPTY -
Check the
Wrap aroundoption -
Select the
Regular expressionsearch mode -
Click on the
Replace Allbutton
=> You should be left with this text :
<start string> blo blo <target string> <end string> <start string> <target string> blu blu <end string> <start string> <target string> <end string>You can verify that, in this OUTPUT :
-
Any text outside the blocks
<start string>....<end string>have been deleted -
Text within blocks
<start string>....<end string>which do not contain the line<target string>have been deleted, too -
It just remains blocks starting with a
<start string>line and ending with a<end string>line which do contain a line<target string> -
And note that, in this last case, all the lines of these blocks are kept, too !
However, with the text provided :
-
The delimiters
*Block startand*Block endare different than in your initial post, but this is not a problem -
But the fact that the
<target string>cannot be clearly identify is a BIG problem
Indeed, from your example, how may I know that this block of text must be kept or not ??
I probably miss something …
BR
guy038
-
-
@Mark-Boonie said in Show (or keep) subsets of a file:
I’m changing it a bit for business reasons
@guy038 said:
I probably miss something …
I think the problem is that the block delimiters and target strings contain sensitive information and it would be irresponsible for the original poster to reproduce them in a public forum.
@Mark-Boonie said:
The first and last lines shown are the delimiter lines.
Many characters have special meanings in regular expressions, so we must be careful telling you how to represent your strings if we don’t know exactly what they are.
Can you tell us whether the delimiter strings are always the same? Do they always start at the beginning of a new line?
If each delimiter string is not exactly the same, every time, we need to know enough details to determine the patterns they follow that differentiate them from other lines in the file.
Do the delimiter strings contain any characters other than letters, numbers, spaces? Rather than get into every last detail, if they do contain characters other than letters, numbers and spaces, could they ever contain the specific sequence
\E(backslash followed by capital E)? If they cannot, the strings can be enclosed in a\Q…\Epair to “quote” them so there is no need to worry about exactly what special characters need escaping.@Mark-Boonie said:
The target string would vary, but obviously it’s another hex string.
A hex string (involving only letters, numbers and possibly spaces) will be no problem. But we need to be precise about what “would vary” means.
Do you mean that each time you do this search, you will start with a copy of the whole file and search for a specific target string?
Or do you mean that there will be several different target strings, and you will want to get all the blocks that contain any of them into a single file?
Or something else?
@guy038 is already on this; I have no other recommendations. All I intend with this post is to clarify what information will be needed to implement his solution in the specific case at hand.