Powershell:查找和替换由换行符分割的单词 [英] Powershell: Find and replace words split by newline
本文介绍了Powershell:查找和替换由换行符分割的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个看起来像这样的文本文件:
So I have a text file that looks something like this:
Members : {USER\member1, USER\member2, US
ER\member3, USER\member4, USER
\member5, USER\member6}
我想删除USER\
.下面的代码会删除它,但当它被换行符分割时不会删除它,例如当 US
在一行而 ER\
在另一行时.
and I would like to remove USER\
. The following code removes it but not when it's split by a newline, for example when US
on one line and ER\
on another line.
Foreach-Object { %{$_.Replace('USER\', '') }
把 `n 或 `r
放在里面是行不通的.任何帮助表示赞赏.
Putting `n or `r
in there doesn't work. Any help is appreciated.
推荐答案
试试这个:
PS > ((Get-Content .\t.txt) | % { $_.Trim() }) -join "" -replace "USER\\"
Members : {member1, member2, member3, member4, member5, member6}
如果文本在字符串数组中,请使用您的变量切换 (Get-Content .\t.txt)
.如果您在字符串(不是数组)变量中有文本,请使用:
If the text is in a string-array, switch out (Get-Content .\t.txt)
with your variable. If you have the text in a string(not array) variable, use:
($MYSTRINGVAR.Split("`r`n") | % { $_.Trim() }) -join "" -replace "USER\\"
EDIT 只需修改成员"部分:
EDIT Just modify the "Members" part:
$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:查找和替换由换行符分割的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文