Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 替换换行的问题_Powershell - Fatal编程技术网

Powershell 替换换行的问题

Powershell 替换换行的问题,powershell,Powershell,Iam正在尝试用PowerShell替换以下字符串: ... (" Intel(R) Network Connections 14.2.100.0 "," 14.2.100.0 ") ... 我使用的代码是: Get-Content $logfilepath | Foreach-Object { $_ -replace '`r`n`r`n', 'xx'} | Set-Content $logfilepath_new 但是我没有成功,有人能告诉我,错误在哪里吗?Get content返回一

Iam正在尝试用PowerShell替换以下字符串:

...
("
Intel(R) Network Connections 14.2.100.0
","
14.2.100.0
")
...
我使用的代码是:

Get-Content $logfilepath | 
Foreach-Object { $_ -replace '`r`n`r`n', 'xx'} | 
Set-Content $logfilepath_new

但是我没有成功,有人能告诉我,错误在哪里吗?

Get content返回一个行数组,因此CRLF本质上是您的分隔符。两个背对背的CRLF序列将被解释为当前行的结尾,后跟一个空行,因此任何行对象都不应包含'`r`n`r`n'。多行正则表达式替换可能是更好的选择。

Get content返回一个行数组,因此CRLF本质上是您的分隔符。两个背对背的CRLF序列将被解释为当前行的结尾,后跟一个空行,因此任何行对象都不应包含'`r`n`r`n'。多行正则表达式替换可能是更好的选择。

首先,在替换字符串中使用单引号-

'`r`n`r`n'
这意味着它们被逐字处理,而不是作为换行符,因此您必须使用-

"`r`n`r`n"
要替换,请将文件作为字符串读取并使用replace方法


首先,在替换字符串中使用单引号-

'`r`n`r`n'
这意味着它们被逐字处理,而不是作为换行符,因此您必须使用-

"`r`n`r`n"
要替换,请将文件作为字符串读取并使用replace方法


作为使用PS cmdlet的替代方法:

Get-Content $logfilepath | 
    Foreach-Object -Begin { $content="" } -Process { $content += $_ ; $content += "xx" } -End { $content } | 
    Set-Content $logfilepath_new

作为使用PS cmdlet的替代方法:

Get-Content $logfilepath | 
    Foreach-Object -Begin { $content="" } -Process { $content += $_ ; $content += "xx" } -End { $content } | 
    Set-Content $logfilepath_new

我使用以下代码将somestring替换为换行符:

$nl = [System.Environment]::NewLine
$content = $content.Replace( somestring, $nl )

我使用以下代码将somestring替换为换行符:

$nl = [System.Environment]::NewLine
$content = $content.Replace( somestring, $nl )