簡體   English   中英

使用通配符和反斜杠的Powershell字符串搜索

[英]Powershell string search with wildcard and backslashes

在文件中,我需要多次搜索以下字符串:

e:\\installroot\\Development2_14.07.21.000\\

字符串的第一部分應始終為常數: "e:\\\\installroot\\\\"

但是字符串的第二部分將更改(永遠不會相同),我還需要搜索該字符串並將其替換為新值: "Development2_14.07.21.000"

這是我要更新的文件(reg文件)的示例:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\CompanyName\Services]
"Proc"="w:\\installroot\\Development2_14.07.21.000\\debug\\job.dll,JobServiceThread,interval;30;w:\\installroot\\Development2_14.07.21.000\\debug\\proc.exe deltasets=TEMP"
"Order"="W:\\installroot\\Development2_14.07.18.000\\debug\\order.dll,OrderThread,"
"GBatch"="NULL,ServiceThread,daily;01:10;W:\\installroot\\Development2_14.07.18.000\\debug\\file.exe"

我不確定通配符或正則表達式搜索/替換在這里是否效果最好,因此可以在此處使用一些輸入來確定最佳方法。 注意雙反斜杠。

另外,我更喜歡直接更新注冊表,而不是更新reg文件並進行reg導入,因此希望可以在PowerShell中進行。

另請注意:在我的搜索場景中,我將無法始終使用術語“ Development2_”。 有時可能是“ Development _”,“ Test_”或“ Release_”,或該路徑中的任何內容。 只是需要考慮的事情。

為什么Select-String無法按您的要求運行?

在:

(cat .\\temp.txt | Select-String -Pattern "installroot") | % { $_.ToString() | % { $_ -replace $_.Substring(17), "Some new content" } }

出:

e:\\\\installroot\\\\Some new content\\

在此處插入通用的查找和替換腳本:

(gc C:\temp.txt) | % { 
    if($_ -like "*installroot*") {
        $_ -replace 'installroot\\.+?\\',"installroot\Development_14.07.21.111\"
    }
    else {
        $_
    }
 }| set-content C:\Temp.txt

是的,您是對的,直接使用powershell對注冊表進行這些更改會更容易。 您應該嘗試一下,讓我們知道如何進行...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM