Extract or Delete Lines Matching One or More Strings or Regexes

PowerGREP’s file sectioning feature makes it trivial to extract and delete lines, or any other kind of file section. Follow these steps to extract all lines matching at least one 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 action type to “search”.
  4. In the “file sectioning” list, select “line by line”.
  5. Turn on the option “collect whole sections”. This makes sure lines will be extracted as a whole.
  6. Enter one or more search terms.
  7. Click the Preview button to run the action.

To extract the lines into new files, set the target type to “save one file for each searched file”. Set “between collected text” to “Line break”. Then click the Collect button.

To delete the lines from the original files instead, set the action type to “search and delete” in step 3 above. If you set the file sectioning type to “line by line”, matching lines will be blanked out, but the number of lines in the file will remain the same. If you set it to “line by line (including line breaks), then the lines will be completely deleted, and the number of lines in the file will shrink.

Extract or Delete Lines Matching All Search Terms

The above steps will extract or delete any line that contains at least one of the search terms. If a line must contain all search terms in order to be extracted or deleted, turn on the option “list only sections matching all items”. This option becomes visible after you set the file sectioning type to “line by line”.

For more complex combinations of search terms, see the example about boolean operators.

Extract or Delete Lines Matching None of The Search Terms

To extract or delete those lines that do not contain any of the search terms, turn on the “invert search results” option in the file sectioning. When using a list of search terms, make sure “list only sections matching all terms” is off.

Extract or Delete Lines Not Matching All of The Search Terms

If you turn on both “invert search results” and “list only sections matching all terms” then you will extract or delete all lines that contain none of the search terms as well as all lines that contain some but not all of the search terms.