Boolean Operators “and”, “or”, and “not”

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.

PowerGREP’s Implicit “or”

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.

  1. Select the files you want to search through in the File Selector.
  2. Start with a fresh action.
  3. Set the search type to "list of literal text".
  4. Make sure "list only files matching all terms" is off to imply "or" between the search terms.
  5. Enter the first search term. Click on the green plus button to add additional search terms to the list.
  6. Click the Preview button to run the action.

List Only Files Matching All 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.

  1. Select the files you want to search through in the File Selector.
  2. Start with a fresh action.
  3. Set the search type to "list of literal text".
  4. Make sure "list only files matching all terms" is on to imply "and" between the search terms.
  5. Enter the first search term. Click on the green plus button to add additional search terms to the list.
  6. Click the Preview button to run the action.

This action is available in the PowerGREP5.pgl library as "List files containing all search terms".

List Only Lines Matching All 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.

  1. Select the files you want to search through in the File Selector.
  2. Start with a fresh action.
  3. Set the search type to "list of literal text".
  4. Enter the first search term. Click on the green plus button to add additional search terms to the list.
  5. In the "file sectioning" list, select "line by line".
  6. Turn on the "list only section matching all terms" option that appears after choosing line by line sectioning. This implies "and" between the search terms.
  7. Click the Preview button to run the action.

This action is available in the PowerGREP5.pgl library as "Collect lines containing all search terms".

Combining “and” and “or”

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:

  1. Select the files you want to search through in the File Selector.
  2. Start with a fresh action.
  3. Set the search type to "list of regular expressions".
  4. Make sure "list only files matching all terms" is on to imply "and" between the regular expressions.
  5. Enter the first regular expression Jack|John. If your search terms contain non-alphanumeric characters, make sure to escape characters that have a special meaning in regular expressions.
  6. Click on the green plus button to add the regular expression Sue|Mary|Grace.
  7. Click the Preview button to run the action.

Files or Lines Not Matching Your Terms

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.

  1. Select the files you want to search through in the File Selector.
  2. Start with a fresh action.
  3. Set the action type to "list files".
  4. Turn on the "invert results" option.
  5. Make sure "list only files matching all terms" is off to imply "or" between the search terms. We're searching for "not (a or b)" to exclude both a and b.
  6. Set the search type to "list of literal text".
  7. Enter the first search term that the files must not contain. Click on the green plus button to add additional search terms to the list.
  8. Click the Preview button to run the action.

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:

  1. Select the files you want to search through in the File Selector.
  2. Start with a fresh action.
  3. Set the search type to "list of literal text".
  4. Enter the first search term that the files must not contain. Click on the green plus button to add additional search terms to the list.
  5. In the "file sectioning" list, select "line by line".
  6. Turn on the "invert results" option that appears after choosing line by line sectioning.
  7. Make sure "list only files matching all terms" is off to imply "or" between the search terms. We're searching for "not (a or b)" to exclude both a and b.
  8. Click the Preview button to run the action.

Find Some Terms, Exclude Other Terms

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".

  1. Select the files you want to search through in the File Selector.
  2. Start with a fresh action.
  3. Set the action type to "list files".
  4. Set "filter files" to "disallow any terms to match".
  5. Set the search type for the file filter to "list of literal text".
  6. Enter the first search term that the files must not contain. Click on the green plus button to add additional search terms to the list.
  7. Set the search type for main part of the action to "list of literal text".
  8. Enter the first search term that the files should contain. Click on the green plus button to add additional search terms to the list.
  9. Click the Preview button to run the action.

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".

A and not B