Action Types

The first thing to do on the Action panel is to use the “action type” drop-down list to select the kind of action you want to execute. The action type largely defines which settings are available on the Action panel. The combination of action type and target type determines what PowerGREP does with the search matches found by the main part of the action or with the files in which those matches are found. PowerGREP offers twelve action types:

  1. Simple search
  2. Search
  3. Collect data
  4. Count matches
  5. List files
  6. File or folder name search
  7. File or folder name collect
  8. Rename files or folders
  9. Search and replace
  10. Search and delete
  11. Merge files
  12. Split files

Simple Search

Simple search

The “simple search” action type hides most of the controls that normally give the Action panel its complicated appearance. Use this action type if you just want to search for one or more search terms through a bunch of files and see the results in PowerGREP. The main differences between “simple search” and “search” are that “simple search” does not support filtering files or creating target files and that file sectioning and context only offer a choice between “off” and “line by line”.

Group Identical Matches

Turn on to collect identical search matches only once. Turn off to list all search matches, including identical ones. When grouping identical matches, the Editor will not highlight matches in the source file, and the Results will display matches without context.

List Only Files Matching All Terms

Turn on to list or process only files that match all of the search terms. Turn off to list or process all files matching one or more of the search terms. This option is only available when the search type of the main part of the action is a list or a delimited set of search terms.

Search

Search

The “search” action type extends “simple search” with additional capabilities. You can use the “filter files” action part to exclude files in which one or more search terms can be found. Essentially, you can search for files matching “A and not B” by having the main part of the action search for A and the file filtering search for B. The “file sectioning” and “context” parts of the Action panel also show their full sets of options. At the bottom of the Action panel, you can set target and backup options to save the search matches into one or more files.

At the top of the Action panel, the “search” action type initially shows the same two “list only files matching all terms” and “group identical matches” checkboxes as the “simple search” action type. Additional options appear when you turn on “group identical matches” that are not available with “simple search”. These options are useful when saving search matches into files.

Group Results for All Files

Turn on to produce one set of results for all files searched through. Turn off to produce a separate set of results for each file.

Order of Collected Matches

Select the order in which the collected matches should be saved into the target file.

Minimum Number of Occurrences

Set to 1 to save all matches into their target files, regardless of how many times each match occurs. Set to a number higher than 1 to save only those matches that occur that many times. Matches that occur fewer times are eliminated from the results. They are not saved into target files, they aren't listed on the Results panel, and they aren't highlighted in the Editor.

Example: Extract or delete lines matching one or more search terms

Collect Data

Collect data

The “collect data” action type runs a search just like the “search” action type. It provides all the same options, with an extra edit control in the main part of the action labeled “collect”. There you can enter the text to be collected each time a search match is found. When using regular expressions, you can use the same syntax you use for the replacement text in a search-and-replace. When searching for a list of search terms, you can enter a different text to be collected for each search match. When using a delimited list of search terms, you can enter the search terms and their corresponding texts to be collected as delimited pairs. If you don’t type in any text to be collected for a particular search term, the actual search match is collected, just like the “search” action type does.

If you want to manipulate the text to be collected beyond what can be easily done with capturing groups and backreferences, turn on the “extra processing” to run an extra search-and-replace on the text to be collected for each search match. In the text to be collected and in the extra processing you can use backreferences to named capturing groups from regular expressions in the “filter files”, “file sectioning” and main parts of the action.

Examples: Find email addresses, Collect page numbers, Collect XML Data with entities replaced, Inspect web logs and Extract Google search terms from web logs

Count Maches

Count matches

The “count matches” action type runs a search just like the “search” action type. But instead of displaying the search matches that were found in the files as the results, “count matches” displays the search terms that you entered in the main part of the action along with the number of times that a match was found for that search term. Search terms that have no matches are also listed in the results. When searching for regular expressions, all the matches for each regex are counted towards that regex, even when the matches are not the same.

List Files

List files

The “list files” action type does not require a search term in the main part of the action. If you don’t provide a search term, PowerGREP simply lists all files that you included in the File Selector. If you do provide a search term, PowerGREP searches through the contents of the files to find it. Files that contain the search term are listed, while files that do not contain the search term are not listed. You can do the opposite by turning on “invert search results”. If you want to do both, listing files that match “A and not B”, turn off “invert search results”. Set the main action to search for A and use the “filter files” option to exclude files containing B.

Search matches are never listed in the results. Only file names are. At the top of the Action panel you can choose to list only file names, paths relative to the folder marked in the File Selector, or complete paths. This makes the “list files” action type significantly faster than “simple search” or “search” because “list files” continues with the next file as soon as the first search match is found in a file, while the other two action types always try to find all search matches in each files so they can be listed in the results.

The target options for “list files” actions allow you to save the list of paths into a file as well as copy, move, or delete the files that were found.

Example: Find files not containing a search term

File or Folder Name Search

File or Folder Name Search

Set the action type to “file or folder name search” if you want the main part of the action to search through the names of files rather than their contents. The “what to search through” setting determines whether only the names of files, only the names of folders, or the names of both files and folders are searched through. That setting also determines whether only the file’s name, the file’s path relative to the folder you’ve marked in the File Selector, or the file’s full path is searched through. If you set the target options to save the list of files that was found, then the “what to search though” setting also determines the part of the file’s path that is saved into the target file.

If you turn on “invert results”, then the results show the file or folder paths in which none of the search terms can be found. If you turn on “list only files matching all terms”, you get the file or folder paths in which all the search terms can be found. If you turn on both these options, you get a list of all paths in which none of the search terms or some of the search terms but not all of the search terms can be found.

There are two ways to search through both file names and file contents. The simplest way is to use the “file name search” action type. Use the main part of the action to search through file names, and use the “filter files” option to search through the contents of the files. Filtering files is always done based on the contents of the files, even for “file name search” actions.

The other way to search through both file names and file contents is to use the “include files” and “exclude files” boxes in the File Selector to include or exclude files based on their file names. Then you can use the Action panel to search through the contents of the included files using the “list files” action type or one of the other action types that search through the contents of files.

The target options for “file or folder name search” actions allow you to save the list of paths into a file as well as copy, move, or delete the files and/or folders that were found.

File or Folder Name Collect

File or Folder Name Collect

The “file or folder name collect” action type corresponds with the “file or folder name search” action type just like the “collect data” action type corresponds with the “search” action type. It searches through the names of files and folders but instead of collecting the search matches you can use the replacement string syntax to specify a different text to be collected, which can be further manipulated with “extra processing”. Path placeholders are also very useful with this action type.

Example: Process files in a batch file or script

Rename Files or Folders

Rename files

The “rename files” action type makes the search term in the main part of the action works on file names rather than on file contents. The new name of each file is determined by running a search-and-replace on the file’s name.

The “rename files” action type can do more than just rename files. At the top of the Action panel you can choose if this search-and-replace should be performed on the file’s name only, or on the file’s path relative to the folder marked in the File Selector, or on the file’s complete path. If you search-and-replace through the file’s name only, the file is renamed and stays in the same folder. If you search-and-replace through the path relative to the folder, the file may be moved into a different subfolder of that folder depending on the result of the search-and-replace. If you search-and-replace through the full path, the file can be moved anywhere. It is your responsibility to make sure the result is a valid path. If you don’t, PowerGREP skips files that don’t get a valid path.

Beyond renaming and moving files, you can also copy them by setting the “target file creation” option to “copy files”. You can even add files to .zip archives or extract them from any archive format that PowerGREP supports. If you rename c:\file.txt into d:\archive.zip::file_from_c.txt then file.txt is added to d:\archive.zip under the new name file_from_c.txt. Doing the reverse extracts the file from the archive. PowerGREP uses a double colon to delimit archives from the path of each file inside the archive. The archive’s extension must be one that is configured in the Archive Formats section in the Preferences. Similarly, renaming file.txt into file.txt.bz2 compresses this file, while renaming file.gz to file.txt decompresses that file. You can even decompress and recompress by renaming file.gz into file.txt.bz2. This works for any extension that is configured as a single file compressed format in the Archive Formats preferences.

The “rename files” action type does allow you to search through the contents of each file using the “filter files” settings. You can use this to rename only certain files based on their contents. Even more powerful is to add named capturing groups to the regular expression for “filter files”. You can then use backreferences to that named capturing group in the regular expression and/or replacement text of the main part of the action. This enables you to use a specific part of the file’s contents in its name.

The “what to rename” setting also includes choices to rename folders. If a folder is renamed or moved and files or folders inside that folder are also included in the action, then those files and folders are still processed and can thus be renamed or moved on their own after their parent folder is renamed or moved. So if you rename A to B in folder names only (not folder paths), then the folder C:\A is renamed into C:\B. Its subfolder that was originally C:\A\A is renamed into C:\B\B.

If you set the target type to copy folders then files and folders inside copied folders are copied along with the parent folder. But the names of files and folders inside copied folders are not searched through even if they were marked as included in the File Selector. The action never makes two copies of the same file or folder and never makes copies of copies. So if you copy folders by replacing A with B in folder names only (not folder paths), then the folder C:\A is copied to C:\B. Its subfolder that was originally C:\A\A is copied along as C:\B\A. There will be no additional copy C:\A\B nor will their be a copy of a copy as C:\B\B.

The “extra processing” part of the Action panel is also available. You can use it to run an extra search-and-replace on the replacement text that will be used to rename the file.

Examples: Replace in file names and contents and Rename files based on HTML title tags

Search and Replace

Search and replace

When you select Clear in the Action menu and set the action type to “search and replace” you can easily run a search-and-replace as you would in any text editor that supports regular expressions. Simply type in your search text into the Search box and the replacement text into the Replacement box in the main part of the action and click the Replace button in the toolbar.

But PowerGREP provides a lot more options on the Action panel. The “filter files” settings allow you to restrict the search-and-replace to files in which a separate set of search terms can or cannot be matched. With the “file sectioning” options you can split the files into lines or other sections and search-and-replace each line or section separately. You can also have lines or sections replaced as a whole.

When using regular expressions, you can use backreferences in the replacement text to capturing groups not only from the main part of the action, but also to named capturing groups from the “file sectioning” and “filter files” parts. If capturing groups and backreferences aren’t enough to build up the replacement text, you can turn on “extra processing” to run an extra search-and-replace on each replacement text.

The “context” settings aren’t used to execute the search-and-replace. They are only used to display the results in PowerGREP. Collecting extra context can be very useful if you plan to manually make or revert replacements after previewing or executing the search-and-replace.

Examples: Delete repeated words, Add a header and footer to files, Add line numbers, Insert proper HTML title tags, Replace HTML tags, Replace HTML attributes, Put anchors around URLs that are not already inside a tag or anchor, Replace in file names and contents and Apply an extra search-and-replace to target files

Search and Delete

Search and delete

The “search and delete” action type is essentially the “search and replace” action type without any replacement text. Search matches are replaced with nothing or deleted. There is one extra option that allows you to delete only certain matches.

This action type deletes search matches rather than files. If you want to delete entire files in which search matches are found, use the “list files” action type together with the “delete files” target type.

Matches to Delete

Choose which search matches you want to delete:

Example: Extract or delete lines matching one or more search terms

Merge Files

Merge files

The “merge files” action type can be used with or without search terms in the main part of the action to gather a list of files as described for the “list files” action type. Whenever a file is found to match, the file’s entire contents are saved into a new file. PowerGREP overwrites the file if it existed before the action started. If you specify the same target file for two or more matching files (or even for all files) in a single “merge files” action then those files are merged together into the target file. You can use the “between collected text” settings on the Action panel to specify if any text should appear between the files when they are merged together and whether the target files should have some header or footer text.

If you set the “target creation type to “merge based on search matches” then a box labeled “target file” appears in the main part of the action. This box allows you to enter a replacement text as you would for a search-and-replace, including backreferences and “extra processing”. The difference is that PowerGREP expects your replacement text to be a valid path. That is the path that PowerGREP merges the file into.

Example: Merge web logs by date

Split Files

Split files

The “split files” action type is identical to the “collect data” action type, except for one thing: instead of a text to be collected for each search match you need to provide a full path to a target file for each search match. You can use all the same syntax for backreferences and “extra processing” to build up this target path. The search match is saved into this file. PowerGREP overwrites the file if it existed before the action started. If you specify the same target file for multiple matches in a single “split files” action then they are all saved into that target file. This works even if the matches were found in different source files, so you can essentially split and merge at the same time. You can use the “between collected text” settings on the Action panel to specify if any text should appear between the collected matches and whether the target files should have some header or footer text.

Examples: Split web logs by date, Split logs into files with a certain number of entries and Split database dumps