Many search tools use the boolean operators “and”, “or”, and “not”. Searching for “term1 and term2 and term3” results in a list of files in which all three search terms can be found. Searching for “term1 or term2 or term3” gives a list of files in which at least one of the three search terms occurs. Searching for “term1 and not term2” lists files that contain term1 but not term2, while “(term1 or term2) and not (term3 or term4)” lists files that contain term1 or term2 or both, but not term3 and not term4.
PowerGREP does not use boolean operators, but does offer similar functionality.
When you specify multiple search terms, PowerGREP automatically implies an “or” operator between them. That is, you will get a list of files containing one or more of the search terms.
If you turn on the option “list only files matching all terms”, PowerGREP will give you a list of files containing each search term at least once, as if you used a boolean “and” operator between the search terms. Files containing some but not all of the search terms will not be displayed in the results.
This action is available in the PowerGREP5.pgl library as “List files containing all search terms”.
With the option “list only sections matching all items”, you can find lines or any other kind of section containing each search term at least once, as if you used a boolean “and” operator between the search terms. Lines or sections containing some but not all of the search terms will not be displayed in the results. This option only appears when using file sectioning.
This action is available in the PowerGREP5.pgl library as “Collect lines containing all search terms”.
The “list only files matching all terms” and “list only sections matching all items” options are all-or-nothing options. When both are off, “or” is implied between all search terms. When either is on, “and” is implied between all search terms, at the file level or the section level.
For a combination of “and” and “or”, you will need to use regular expressions. Turn on “list only files/sections matching all items” to imply “and” between the regular expressions, as in the above examples. Then use the alternation regex operator to combine multiple search terms into a single regular expression. Alternation is the regex-equivalent of “or”.
E.g. for the boolean query “(Jack or John) and (Sue or Mary or Grace)”, you would need two regular expressions, as follows:
A boolean search for “not a” or for “not a and not b” gets you a list of files that don’t contain your search term(s). In PowerGREP you can do this with the “invert search results” checkbox in the file sectioning part of the action. If the action type is list files, this option inverts the list of files. You get the list of files that don’t contain any of the search terms anywhere.
For all other action types, the option is only available when dividing your files into sections using PowerGREP’s file sectioning feature. Inverting the results then gives you a list of sections that don’t contain the search terms. You can get all lines not containing any of your search terms as follows:
You can emulate the boolean combination “and not” using PowerGREP’s ability to use a second set of search terms to filter files prior to the actual search. The boolean query “(term1 or term2) and not (term3 or term4)” gets you a list of files that contain term1 or term2 or both, but not term3 and not term4. In PowerGREP, “term1 or term2” is the main search and “not (term3 or term4)” is the filtering condition. PowerGREP first searches for “term3 or term4”. If those can’t be found in a file, then PowerGREP searches for “term1 or term2”.
PowerGREP supports only one “and not”. If you have “term1 and not term2 and term3 and not term4” you need to rewrite the boolean query first. First, put all the negated terms together: “term1 and term3 and not term2 and not term4”. Then use boolean algebra to put the “not” outside parentheses so only one “not” remains: “term1 and term3 and not (term2 or term4)”. Now you can create a PowerGREP action for this using the steps above. To make the file match “term1 and term3”, turn on “list only files matching all terms”.
The screen shot below shows what the Action panel looks like when you follow the above steps to find “A and not B”.