使用powershell和regex查找并替换文本文件中的多个字符串

使用powershell和regex查找并替换文本文件中的多个字符串,regex,powershell,replace,Regex,Powershell,Replace,大家好,提前感谢你们的帮助 我在一个txt文件中有一个相同链接的列表,需要在其中的多行中替换一个字符串“username”:文件中一行的示例(替换为粗体的字符串) 文件中的用户列表:示例C:\Temp\names.txt。文件结构如:john bob merry等 txt文件的输出应如下所示: 所有替换内容都应取自txt文件 此代码不起作用,它只是一个示例: $RUSER=Select-String -Pattern "username" -AllMatches | %

大家好,提前感谢你们的帮助

我在一个txt文件中有一个相同链接的列表,需要在其中的多行中替换一个字符串“username”:文件中一行的示例(替换为粗体的字符串)

文件中的用户列表:示例C:\Temp\names.txt。文件结构如:john bob merry等

txt文件的输出应如下所示:

所有替换内容都应取自txt文件

此代码不起作用,它只是一个示例:

$RUSER=Select-String -Pattern "username" -AllMatches | % {  $_.Matches } | % { $_.Groups[1].value} $RUSER > $output_file
$pattern='username'
$url=”https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/**用户名**@svgauth%3Fsecret%3D”
$names=[string[](获取内容$home\names.txt)。拆分(“”)
$names |%{[regex]::替换($url,$pattern,$|)}输出文件$home\resultNames.txt

names.txt
包含一行多个用户名并用空格分隔,还是包含多行(即每行一个用户名)?我猜
用户名是可变的,就像
FletcherF1@svgauth...
这肯定不是
用户名
字面意思。作者:“文件中的用户列表:示例C:\Temp\names.txt。文件结构,如:约翰·鲍勃·梅里等等……”我的答案是可行的fine@Jan我通过添加$pattern变量来更正我的答案。
$pattern = 'username' <#write your pattern there#>
$url = "https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/**username**@svgauth%3Fsecret%3D"
$names =[string[]](get-content $home\Names.txt).Split(' ')
$names | % {[regex]::Replace($url,$pattern,$_)} | Out-File $home\resultNames.txt