powershell - Powershell:查找并替换由换行符分隔的单词

标签 powershell

所以我有一个看起来像这样的文本文件:

Members : {USER\member1, USER\member2, US
           ER\member3, USER\member4, USER
           \member5, USER\member6}

而且我想删除USER\。以下代码将其删除,但在将其用换行符分隔时将其删除,例如,当一行上的US和另一行上的ER\时,将其删除。
Foreach-Object { %{$_.Replace('USER\', '') }

在其中放置`n or `r不起作用。任何帮助表示赞赏。

最佳答案

试试这个:

PS > ((Get-Content .\t.txt) | % { $_.Trim() }) -join "" -replace "USER\\"
Members : {member1, member2, member3, member4, member5, member6}

如果文本位于字符串数组中,请使用您的变量切换出(Get-Content .\t.txt)。如果文本在字符串(不是数组)变量中,请使用:
($MYSTRINGVAR.Split("`r`n") | % { $_.Trim() }) -join "" -replace "USER\\"

编辑只需修改“成员”部分:
$text = (Get-Content .\input.txt) -join "`r`n"
($text | Select-String '(?s)(?<=Members : \{)(.+?)(?=\})' -AllMatches).Matches | % {
        $text = $text.Replace($_.Value, ($_.Value -split "`r`n" | % { $_.Trim() }) -join "" -replace "USER\\")
}
$text | Set-Content output.txt

关于powershell - Powershell:查找并替换由换行符分隔的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14840632/

相关文章:

object - 解析一个对象,为每个成员添加一个成员属性

powershell - 如何使用 PowerShell 脚本禁用所有 BizTalk 主机实例

math - PowerShell 数学问题?

python - PowerShell 在大型搜索/替换操作中很慢(比 Python 慢得多)?

.net - Powershell SQL Server 2008远程数据库备份

c# - 在WPF应用程序中使用powershell通过uninstallString卸载应用程序

powershell - 创建并抛出新异常

powershell - 如何在 Powershell 中列出 .zip 文件的内容?

powershell - 在 PowerShell 中,如何获取有关 "more"命令用法的帮助?

arrays - Powershell数组长度