2

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.

3 Answers 3

2

You can use the Split method from the regex class:

$file = Get-Content C:\file.txt
foreach ($line in $file) {
    [regex]::split($line, '///')[1]
}

And the result, you can save it to the same file or another.

3
  • 1
    You could also replace each line with nothing. $line.replace(".*///","") should do it. It can then be piped to a file. Oct 15, 2018 at 2:59
  • That works, but a more PowerShell-idiomatic solution is to use the -split operator: ($line -split '///')[1]. If you do want to use .NET directly, $line.split('///')[1] is more efficient, as it uses string-literal-based splitting.
    – mklement0
    Oct 15, 2018 at 3:24
  • 1
    @RobertCotterman: I assume you meant $line -replace '.*///', ''. The [string] type's .Replace() method only supports literal search strings, not regexes.
    – mklement0
    Oct 15, 2018 at 3:25
1

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
0

try this:

Get-Content "C:\temp\test.txt" | %{($_ -split ': ')[1] -replace '/New folder/', '/New%20folder/'} | select -Unique
1
  • While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find how to write a good answer very helpful. Please edit your answer.
    – hellow
    Oct 15, 2018 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.