如何用命令行替换文本文件中的每个前缀?
How to replace every prefix in a text file with command line?
我有一个看起来像这样的文本文件:
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
我最终希望它看起来像这样:
C:/Users/igues/Desktop/New%20folder/NOTEPAD.exe
C:/Program Files/Everything/Everything.exe
C:/Program Files/Notepad++/notepad++.exe
如何使用命令行(或 PowerShell)删除烦人的前缀?我已经知道如何删除重复的行。我只需要删除每行开头的“0x???????? (???): file:///C:/”前缀。
已编辑以修复前缀。
您可以使用 regex class:
中的 Split 方法
$file = Get-Content C:\file.txt
foreach ($line in $file) {
[regex]::split($line, '///')[1]
}
以及结果,您可以将其保存到同一个文件或另一个文件中。
PowerShell 基于正则表达式的 -replace
operator 非常适合前缀剥离:
(Get-Content file.txt) -replace '^.+///'
Regex ^.+///
匹配一个或多个 (+
) 个字符 (.
) 从开头 (^
) 到 ///
并且 - 由于缺少替换字符串 - 默认情况下用 空字符串 替换匹配的字符串,即 删除 它。
请注意,Get-Content
将文本文件的 行 输出为 数组 ,这会导致 -replace
运行分别在每一行(数组元素)上,结果也作为数组返回。
使用数组文字的简单演示:
# 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 '^.+///'
输出(也是一个 2 元素数组):
C:/Users/igues/Desktop/New folder/NOTEPAD.exe
C:/Program Files/Everything/Everything.exe
试试这个:
Get-Content "C:\temp\test.txt" | %{($_ -split ': ')[1] -replace '/New folder/', '/New%20folder/'} | select -Unique
我有一个看起来像这样的文本文件:
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
我最终希望它看起来像这样:
C:/Users/igues/Desktop/New%20folder/NOTEPAD.exe
C:/Program Files/Everything/Everything.exe
C:/Program Files/Notepad++/notepad++.exe
如何使用命令行(或 PowerShell)删除烦人的前缀?我已经知道如何删除重复的行。我只需要删除每行开头的“0x???????? (???): file:///C:/”前缀。
已编辑以修复前缀。
您可以使用 regex class:
中的 Split 方法$file = Get-Content C:\file.txt
foreach ($line in $file) {
[regex]::split($line, '///')[1]
}
以及结果,您可以将其保存到同一个文件或另一个文件中。
PowerShell 基于正则表达式的 -replace
operator 非常适合前缀剥离:
(Get-Content file.txt) -replace '^.+///'
Regex ^.+///
匹配一个或多个 (+
) 个字符 (.
) 从开头 (^
) 到 ///
并且 - 由于缺少替换字符串 - 默认情况下用 空字符串 替换匹配的字符串,即 删除 它。
请注意,Get-Content
将文本文件的 行 输出为 数组 ,这会导致 -replace
运行分别在每一行(数组元素)上,结果也作为数组返回。
使用数组文字的简单演示:
# 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 '^.+///'
输出(也是一个 2 元素数组):
C:/Users/igues/Desktop/New folder/NOTEPAD.exe
C:/Program Files/Everything/Everything.exe
试试这个:
Get-Content "C:\temp\test.txt" | %{($_ -split ': ')[1] -replace '/New folder/', '/New%20folder/'} | select -Unique