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.
- Select the files you want to search through in the File Selector.
- Start with a fresh action.
- Set the action type to “search and replace”. Leave the search type as “regular expression”.
- 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.
- In the Replace box, enter /home/me\1
- Tick the extra processing checkbox. An additional set of controls for entering search terms appears.
- Set the extra processing search type to “delimited literal text”.
- Enter a single semicolon in the “extra item delimiter” field, and a single equals sign in the “extra pair delimiter” field.
- In the “extra processing search” box, enter \=/; =_ to substitute backslashes with forward slashes, and spaces with underscores.
- Click the Preview button to run a test.
- 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:
- The regular expression of the main action matches c:\My Documents\Test Files\Path Test.txt
- The backreference in the replacement text of the main action is expanded. The replacement becomes /home/me\Test Files\Path Test.txt
- 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
- The main action deletes the search match from the file, and substitutes it with the new replacement text.
- 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”.