Convert Windows to UNIX Paths

PowerGREP's "extra processing" feature makes it very easy to search through text files and replace file references in those files from Windows paths into UNIX paths. The example replaces all references to files under c:\My Documents\ into /home/me/, converting backslashes into forward slashes and spaces into underscores.

  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 and replace". Leave the search type as "regular expression".
  4. In the Search box, enter the regular expression c:\\My Documents([^\t\r\n<>|/:"]*[^\s<>|/:"]) and make sure to leave "case sensitive search" off. The regex matches a Windows path under c:\My Documents. The second character class makes sure that a space after the path is not matched as part of the path.
  5. In the Replace box, enter /home/me\1
  6. Tick the extra processing checkbox. An additional set of controls for entering search terms appears.
  7. Set the extra processing search type to "delimited literal text".
  8. Enter a single semicolon in the "extra item delimiter" field, and a single equals sign in the "extra pair delimiter" field.
  9. In the "extra processing search" box, enter \=/; =_ to substitute backslashes with forward slashes, and spaces with underscores.
  10. Click the Preview button to run a test.
  11. If all looks well, click the Replace button to actually replace the paths.

Technically, this action consists of two search-and-replace operations. The one you define first is the main action. It searches through the files you marked in the File Selector. The "extra processing" search-and-replace is applied each time the main action finds a match. Extra processing does not search through any files, but makes replacements in the replacement text of the main action, just before the main action substitutes the search match in the file.

An example will make this clear. If you apply the above action to a single file containing the text The path c:\My Documents\Test Files\Path Test.txt will be converted, PowerGREP does the following:

  1. The regular expression of the main action matches c:\My Documents\Test Files\Path Test.txt
  2. The backreference in the replacement text of the main action is expanded. The replacement becomes /home/me\Test Files\Path Test.txt
  3. The extra processing part of the action is invoked on the replacement. It makes 4 substitutions, replacing two spaces with underscores, and two backslashes with forward slashes. The new replacement text for the main action becomes /home/me/Test_Files/Path_Test.txt
  4. The main action deletes the search match from the file, and substitutes it with the new replacement text.
  5. The whole process is repeated from step 1 for all remaining search matches in the file. There are none in this example.

The end result is The path /home/me/Test_Files/Path_Test.txt will be converted

You can find this action in the PowerGREP5.pgl standard library as "Convert Windows paths into UNIX paths".