Search code examples
regexwindowspowershellbatch-fileprefix

How to replace every prefix in a text file with command line?


I have a text file that looks something like this:

0x1cb139c0 (110): file:///C:/Users/igues/Desktop/New folder/NOTEPAD.exe
0x1cb13f40 (110): file:///C:/Users/igues/Desktop/New folder/NOTEPAD.exe
0x1cb14bc0 (110): file:///C:/Users/igues/Desktop/New folder/NOTEPAD.exe
0x1cb38fc0 (104): file:///C:/Program Files/Everything/Everything.exe
0x1cb39fc0 (104): file:///C:/Program Files/Everything/Everything.exe
0x1cb3a040 (104): file:///C:/Program Files/Everything/Everything.exe
0x1cb43730 (100): file:///C:/Program Files/Notepad++/notepad++.exe
0x1cb44300 (100): file:///C:/Program Files/Notepad++/notepad++.exe
0x1cb44b50 (100): file:///C:/Program Files/Notepad++/notepad++.exe

I eventually want it to look like this:

C:/Users/igues/Desktop/New%20folder/NOTEPAD.exe
C:/Program Files/Everything/Everything.exe
C:/Program Files/Notepad++/notepad++.exe

How can I remove that annoying prefix using the command line (or PowerShell)? I already know how to remove duplicate lines. I just need to remove this "0x???????? (???): file:///C:/" prefix at the start of every line.


Edited to fix prefix.


Solution

  • PowerShell's regex-based -replace operator lends itself well to prefix-stripping:

    (Get-Content file.txt) -replace '^.+///'
    

    Regex ^.+/// matches one or more (+) characters (.) from the beginning (^) through /// and - due to absence of a replacement string - by default replaces the matched string with the empty string, i.e., removes it.

    Note that Get-Content outputs a text file's lines as an array, which causes -replace to operate on each line (array element) individually, with the results also being returned as an array.

    A simple demonstration with an array literal:

    # Sample input lines, as would be returned from a Get-Content call.
    $lines = 
      '0x1cb139c0 (110): file:///C:/Users/igues/Desktop/New folder/NOTEPAD.exe',
      '0x1cb38fc0 (104): file:///C:/Program Files/Everything/Everything.exe'
    
    $lines -replace '^.+///'
    

    Output (also a 2-element array):

    C:/Users/igues/Desktop/New folder/NOTEPAD.exe
    C:/Program Files/Everything/Everything.exe