将文本文件中的“"(”替换为名称为“Tr”的PowerShell文本文件。

huangapple go评论65阅读模式
英文:

Replace "( with name Tr in powershell text file

问题

如何替换这两个特殊字符,被视为一个 -> "(Tr

Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object -Process {
    (Get-Content $_) -Replace ''''"('''', 'Tr'| Set-Content $_
}

需要帮助。

英文:

How do we replace these two special characters considered as one -> "( to Tr.

Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object -Process {
    (Get-Content $_) -Replace '''"(''', 'Tr'| Set-Content $_
}

kindly need assistance

答案1

得分: 0

使用-replace,您需要使用\转义(,以便按原样匹配它,因为它是正则表达式的特殊字符之一。请参阅转义字符

Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object {
    (Get-Content $_ -Raw) -replace '\"\(\' , 'Tr' | Set-Content $_
}

使用.Replace, 字符串方法会更简单,其中一切都按原样匹配,无需转义:

Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object {
    (Get-Content $_ -Raw).Replace('\"(' , 'Tr') | Set-Content $_
}
英文:

With -replace you would need to escape the ( with \ to match it literally as it is a regex special character. See Escaping characters.

Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object {
    (Get-Content $_ -Raw) -replace '"\(', 'Tr' | Set-Content $_
}

It's much easier with .Replace, the String method, where everything is matched literally and no escaping is needed:

Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object {
    (Get-Content $_ -Raw).Replace('"(', 'Tr') | Set-Content $_
}

huangapple
  • 本文由 发表于 2023年5月17日 19:49:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定