我正在尝试编写一个脚本来搜索文件内容,并在找到ASCII控制字符组时插入CR / LF。
我想要替换的字符模式是[ETX][NUL][STX][ETX][SOH]
$filenames = @(Get-Childitem "E:\VendorFiles\*")
$CR = @("[char]3 [char]0 [char]2 [char]3 [char]1") foreach ($file in $filenames) { $outfile = "$file" + ".txt" Get-Content $file | Foreach-object { $_ -replace $CR,"`r`n" ` -replace [char]3,"|" ` -replace [char]1,"{" ` -replace "\\","\\" ` } | Set-Content -encoding "UTF8" $outfile }
-replace
不支持在 RHS 上使用数组(只支持一个 regex_);但是,_LHS - 输入字符串 - 可以是一个数组(在这种情况下,替换操作将分别在数组元素上执行)。 - mklement0