2015-12-15 65 views
1

我有內容存儲在文本文件中。在這25行低於含量如何用特殊符號換新行替換特殊符號行

$userid = $null 

現在我想通過

$userid = "Chandru" 

我嘗試下面的代碼替換該行,但它並沒有幫助我。

$content = Get-content c:\content.text 
$oldline = "`$userid = `$null" 
$newline = "`$userid = `"chandru`"" 
$newcontent = $content -replace ("$oldline","$newline") 

這對我不起作用。

回答

1

"`$userid"爲PowerShell轉義$。你需要躲避$正則表達式:

$oldline = '\$userid = \$null' 
$newline = '$userid = "chandru"' 
(Get-Content 'C:\content.text') -replace $oldline, $newline 

您可以使用[regex]::Escape()如果你想逃避字符串中的所有特殊字符。