1

我有一个文件。我需要选择一些连续的字符串,加入它们并将它们放在文件中,放在powershell脚本中选定字符串的第一个位置

我尝试:

$test=cat $file |select-string -pattern '<DIV>$' -Context 0,1|%{$_ -split "\r\n"}
$t= -join ($test[0],$test[1])
cat  $file|%{$_ -replace '$test','$t'} > temp.txt

或这个

cat $file | %{$_ -replace '<DIV>[\u000d\u000a]{0,2}',""}>temp.txt

但失败

4

1 回答 1

0

从您显示的代码中,我得出结论,您想在 div 标记后删除换行符。

cat 作为 get-content 的别名返回一个数组,因此换行符由数组输出重置为文件。当然有严格的 powershel cmdlet 方法可以做到这一点。

我发现最快的是:

[io.file]::readAllText("$file") -replace "<DIV>`r`n" "<DIV>" > temp.txt

readAlltext保留文件的换行符,因此替换有效。

高温高压

于 2012-07-16T22:54:18.430 回答