在行末端替换文本字符串的脚本

发布于 2025-02-13 06:53:54 字数 700 浏览 0 评论 0原文

如果

((Get-Content -path "C:\Users\User1\OUT\Summary.txt" -Raw) -replace '</ab></cb>','</x>') | Set-Content -Path "C:\Users\User1\OUT\Summary.txt"

可能

C:\Users\User1\File\Summary.txt

​;&lt; b&gt; data

,同时以:&lt;/ab gt;&lt;/cb&gt;

来更改结局到:&lt;/x&gt; 它需要保存文件而不更改文件名。

例如,显示此数据的行:

<a><b>Data:</y> 12345678</ab></cb>

将更改为:

<a><b>Data:</y> 12345678</x>

上面的PowerShell脚本将找到&lt;/ab gt;&gt;/cb&gt;的所有实例,并用&lt;/x&gt替换它;,这不是我希望完成的。

I would like to modify this script if possible:

((Get-Content -path "C:\Users\User1\OUT\Summary.txt" -Raw) -replace '</ab></cb>','</x>') | Set-Content -Path "C:\Users\User1\OUT\Summary.txt"

I would like a script that will run with Windows OS to search through one file it finds at this path:

C:\Users\User1\File\Summary.txt

And within that file, when it finds data starting with: <a><b>Data

And at the same time ending with: </ab></cb>

It would need to change the ending to: </x>
And it would need to save the file without changing the name of the file.

For instance a line showing this data:

<a><b>Data:</y> 12345678</ab></cb>

Would be changed to:

<a><b>Data:</y> 12345678</x>

The PowerShell script above will find all instances of </ab></cb> and replace it with </x>, which is not what I am hoping to accomplish.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拍不死你 2025-02-20 06:53:54

您可以使用get-content处理文件行是行,只有在on &lt上拥有match时,才能替换; a&gt;&lt; b&gt;。这样的事情:

$InFile = ".\TestIn.txt"
$OutFile = ".\TestOut.txt"
If (Test-Path -Path $OutFile) {Remove-Item $OutFile}

Get-Content $InFile | ForEach-Object -Process {
  $NewLine = $_
  If ($_ -Match '<a><b>') {
    $NewLine = ($_ -Replace '</ab></cb>','</x>')
    }
  Add-Content $OutFile $NewLine
}

You can use Get-Content to process the file line be line and only do the Replace when you have a Match on <a><b>. Something like this:

$InFile = ".\TestIn.txt"
$OutFile = ".\TestOut.txt"
If (Test-Path -Path $OutFile) {Remove-Item $OutFile}

Get-Content $InFile | ForEach-Object -Process {
  $NewLine = $_
  If ($_ -Match '<a><b>') {
    $NewLine = ($_ -Replace '</ab></cb>','</x>')
    }
  Add-Content $OutFile $NewLine
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文